2012-08-17 2 views
0

Я разрабатываю приложение для своего сайта, на котором есть RSS-канал с парсером. Я сделал кнопку обновления, чтобы искать новые сообщения, и это работает. Но теперь я хочу, чтобы он отображал UIAlertView, в котором говорится «Нет сообщений», если не было обнаружено новых сообщений.Показать UIAlertView, если новые сообщения не найдены

Это моя кнопка обновления

- (IBAction)refreshButton:(UIBarButtonItem *)sender 
{ 

    // Create a new data container for the stuff that comes back from the service 
    xmlData = [[NSMutableData alloc] init]; 

    // Construct a URL that will ask the service for what you want - 
    // Note we can concatenate literal strings together on multiple lines in this way it 
    // results in a single NSString instance 
    NSURL *url = [NSURL URLWithString: 
        @"http://sephardijews.com/feed/"]; 

    // Putting the URL we made into an NSURLRequest, so we can connect to the url data that we specifed 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 

    // Creating a connecting that will exchange this request for the data from the URL we specifed 
    connection = [[NSURLConnection alloc] initWithRequest:req 
               delegate:self 
             startImmediately:YES]; 

    [[self tableView] reloadData]; 
    NSLog(@"%@\n %@\n %@\n", channel, [channel title], [channel infoString]); 

} 

Как я мог это сделать? Что-то с утверждением if?

Кнопка обновления:

- (IBAction)refreshButton:(UIBarButtonItem *)sender 
{ 

    // Create a new data container for the stuff that comes back from the service 
    xmlData = [[NSMutableData alloc] init]; 

    // Construct a URL that will ask the service for what you want - 
    // Note we can concatenate literal strings together on multiple lines in this way it 
    // results in a single NSString instance 
    NSURL *url = [NSURL URLWithString: 
        @"http://sephardijews.com/feed/"]; 

    // Putting the URL we made into an NSURLRequest, so we can connect to the url data that we specifed 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 

    // Creating a connecting that will exchange this request for the data from the URL we specifed 
    connection = [[NSURLConnection alloc] initWithRequest:req 
               delegate:self 
             startImmediately:YES]; 

    [[self tableView] reloadData]; 
    NSLog(@"%@\n %@\n %@\n", channel, [channel title], [channel infoString]); 

    if ([[self tableView] numberOfRowsInSection:0] > someNumberVariableForLastCount) { 
     someNumberVariableForLastCount = [[self tableView] numberOfRowsInSection:0]; 
     [[self tableView] reloadData]; 
    }else{ 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No New Posts" message:@"There were no new posts found" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil]; 
     [alert show]; 
    } 
} 
+3

[? Что вы пробовали] (http://mattgemmell.com/2008/12/ 08/what-have-you-try /) –

ответ

0

Ваш reloadData вызов должен произойти в connectionDidFinishLoading, а не сразу после начала соединения (это асинхронный сетевой вызов, то таблица будет перезагружен, прежде чем что-либо даже неправдоподобным). Предупреждение также должно быть начато. Вам нужно реализовать все материалы делегирования соединения, надеюсь, вы это сделали. Если нет, базовый пример here.

0

Это будет сравнить текущее количество строк в разделе указать на переменную для последнего подсчета:

if ([[self tableView] numberOfRowsInSection:0] > someNumberVariableForLastCount) { 
      someNumberVariableForLastCount = [[self tableView] numberOfRowsInSection:0]; 
      [[self tableView] reloadData]; 
     }else{ 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"no new items" message:@"OH NO!!" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil]; 
      [alert show]; 
     } 
+0

Какой должен быть последний счет? – jakife

+0

Вы хотите объявить его в своем .h, но затем, вероятно, сначала установите его в 'viewDidLoad' как 0:' int someNumberVariableForLastCount = 0; ' –

+0

@YuvalMarcus Если мой ответ помог вам, пожалуйста, не забывайте отмечать его как правильно :) –

Смежные вопросы