【问题标题】:Display a UIAlertView if no new posts were found如果没有找到新帖子,则显示 UIAlertView
【发布时间】:2012-08-14 06:34:32
【问题描述】:

我正在为我的网站开发一个应用程序,该应用程序具有带有解析器的 RSS 提要。我做了一个刷新按钮来查找新帖子,它可以工作。但是现在,如果没有找到新帖子,我希望它显示一个 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];
    }
}

【问题讨论】:

标签: objective-c uitableview rss uialertview nsxmlparser


【解决方案1】:

这会将您指定的部分中的当前行计数与最后计数的变量进行比较:

  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];
        }

【讨论】:

  • 最后的计数应该是多少?
  • 您需要在 .h 中声明它,但随后可能在 viewDidLoad 中将其初始设置为 0,如下所示:int someNumberVariableForLastCount = 0;
  • @YuvalMarcus 如果我的回答对您有所帮助,请记得将其标记为正确:)
  • 我更新了它,因为一开始有一个错误。现在一切正常!谢谢你
  • 其实有个小bug。当我单击刷新按钮并且没有新帖子时,它不会显示警报。只有第二次才会这样做。你知道如何解决这个问题吗?
【解决方案2】:

您的reloadData 调用应该发生在connectionDidFinishLoading 中,而不是在您开始连接之后(这是一个异步网络调用,甚至在获取任何内容之前都会重新加载表)。警报也应该在那里启动。您需要实现所有连接委托的东西,希望您已经做到了。如果没有,基本示例here

【讨论】:

    猜你喜欢
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    相关资源
    最近更新 更多