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

问题描述

| 我有一个UITableview,顶部带有导航栏。我有一个刷新按钮作为rightBarButtonItem。 单击刷新按钮后,我想隐藏刷新按钮,重新加载表格并显示一个Alertview。
-(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\'
    

解决方法

基本上,虽然您没有设置
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];
}
    ,为什么不禁用刷新按钮呢,它更容易,而且就我个人而言,我假设用户会期望。它是在众多软件应用程序中使用的范例。如果我触摸一个按钮,我真的希望它消失吗,为什么消失了,它坏了吗?如果已将其禁用,并且用户可以看到正在发生的事情(活动指示器,警报框-可能是过度杀伤力?),那么他们将更加确信您的应用以可预测和可靠的方式运行
myButton.isEnabled = NO;
//在执行任务时设置活动指示器 //完成后
myButton.isEnabled = YES;