延迟隐藏导航右栏按钮项目

时间:2011-05-26 20:37:38

标签: iphone objective-c xcode uitableview

我有一个UITableview,顶部有一个导航栏。我有一个刷新按钮作为rightBarButtonItem。

单击刷新按钮时,我想隐藏刷新按钮,重新加载表格并显示警报视图。

-(void)refreshClicked{
    self.navigationItem.rightBarButtonItem=nil;
    app.networkActivityIndicatorVisible = YES;
    [appDelegate readJSONData];
    [self.tableView reloadData];
    UIAlertView *infoAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"Kampanjerna är nu uppdaterade" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [infoAlert show];
    [infoAlert release];
}   

我发现当我的wifi信号变弱时,刷新按钮不会立即隐藏,并且会有延迟。我担心如果使用3G会有进一步的延迟,用户可能会再次按下刷新按钮,这是第一次没有按下。

我的代码有问题吗?

帮助将不胜感激

编辑-----------

-(void)refreshClicked{
    self.navigationItem.rightBarButtonItem=nil;
    app.networkActivityIndicatorVisible = YES;

    // do data processing in the background
    [self performSelectorInBackground:@selector(doBackgroundProcessing) withObject:self];

    UIAlertView *infoAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"Kampanjerna är nu uppdaterade" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [infoAlert show];
    [infoAlert release];
}


- (void)doBackgroundProcessing {
    NSAutoreleasePool*pool=[[NSAutoreleasePool alloc] init]; 
    [appDelegate readJSONData]; 

    // must update the UI from the main thread only; equivalent to [self.tableView reloadData]; 
    [self performSelectorOnMainThread:@selector(reloadData) withObject:self.tableView waitUntilDone:NO];

    [pool release];
}

错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[campaignTableViewController reloadData]: unrecognized selector sent to instance 0x703eba0'

2 个答案:

答案 0 :(得分:0)

基本上,虽然你没有rightBarButtonItem,但是在控件返回到应用程序的主运行循环之前,该更改不会反映在UI中。因此,如果您的方法的其余部分需要一些明显的时间(就像网络请求一样),那么在完成该工作之后,您将看不到按钮消失。

更直接:你正在阻止主线程;要修复它,你需要在后台线程上进行耗时的工作。

这样的事情应该有效(既不编译也不测试):

-(void)refreshClicked{
    self.navigationItem.rightBarButtonItem=nil;
    app.networkActivityIndicatorVisible = YES;

    // do data processing in the background
    [self performSelectorInBackground:@selector(doBackgroundProcessing) withObject:self];

    // go ahead and show the alert immediately
    UIAlertView *infoAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"Kampanjerna är nu uppdaterade" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [infoAlert show];
    [infoAlert release];
}   

- (void)doBackgroundProcessing {
    [appDelegate readJSONData];

    // must update the UI from the main thread only; equivalent to [self.tableView reloadData];
    [self performSelectorOnMainThread:@selector(reloadData) withObject:self.tableView waitUntilDone:NO];
}

答案 1 :(得分:0)

为什么不禁用刷新按钮,它更容易,个人就是我认为用户期望的。它是众多软件应用程序中使用的范例。如果我触摸一个按钮,我真的希望它消失,它为什么会消失,它是否已经破碎?如果它被禁用并且用户可以看到正在发生的事情(活动指示器,警报框 - 可能是过度杀伤?)那么他们会更加确信您的应用程序以可预测和可靠的方式运行

myButton.isEnabled = NO;

//在你做的时候设置活动指标

//完成后

myButton.isEnabled = YES;
相关问题