2011-05-26 88 views
0

我有一个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' 

回答

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

感谢您的回复时完成。我是iphone开发新手。你能告诉我一个例子吗? – Neelesh 2011-05-26 20:44:51

+0

除非你打算换掉另一个按钮,否则你可能应该采纳@PenOne的建议。 – bosmacs 2011-05-26 21:42:47

+0

这是我得到的错误。 *** __NSAutoreleaseNoPool():类NSURL的对象0x9c06910自动释放,没有池到位 - 只是漏水 – Neelesh 2011-05-26 23:10:53

0

为什么不禁止的刷新按钮来代替,它容易得多,而且个人是我会假定用户期望的那样。它是众多软件应用程序中使用的范例。如果我触摸一个按钮,我真的希望它消失,为什么它消失了,它是否坏了?如果它被禁用,用户可以看到某些事情正在发生(活动指示灯,提示框 - 也许矫枉过正?),那么他们会更自信,你的应用程序在一个可预见的和可靠的方式

myButton.isEnabled = NO; 

//一套行为当你做你的东西活动的指标

//后来

myButton.isEnabled = YES;