简体   繁体   English

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

[英]delay in hiding navigation right bar button item

I have a UITableview with a navigation bar on the top.我有一个顶部有导航栏的 UITableview。 I have a refresh button as the rightBarButtonItem.我有一个刷新按钮作为 rightBarButtonItem。

When the refresh button is clicked i want to hide the refresh button, reload the table and display an 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];
}   

I find that when my wifi signal gets weaker, the refresh button is not hidden immediately and there is a delay.我发现当我的wifi信号变弱时,刷新按钮并没有立即隐藏,并且有延迟。 I am afraid if 3G is used there will be further delays and the user might press the refresh button again, thinking first time it wasnt pressed.恐怕如果使用 3G 会有进一步的延迟,用户可能会再次按下刷新按钮,以为第一次没有按下它。

IS there some problem with me code?我的代码有问题吗?

Help would be appreciated帮助将不胜感激

EDIT-----------编辑 - - - - - -

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

Error错误

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

Basically, although you nil rightBarButtonItem , that change won't be reflected in the UI until control returns to the application's main run loop.基本上,尽管您为零rightBarButtonItem ,但在控制返回到应用程序的主运行循环之前,该更改不会反映在 UI 中。 So if the rest of your method takes some noticeable time (like it would with network requests), you won't see the button go away until after that work is done.因此,如果您的方法的 rest 需要一些明显的时间(就像处理网络请求一样),您将不会看到按钮 go 消失,直到完成该工作。

More directly: you're blocking the main thread;更直接:你阻塞了主线程; to fix it, you need to do the time-consuming work on a background thread.要修复它,您需要在后台线程上进行耗时的工作。

Something like this ought to work (neither compiled nor tested):像这样的东西应该可以工作(既没有编译也没有测试):

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

why not disable the refresh button instead, its much easier, and personally is what i would assume a user would expect.为什么不禁用刷新按钮,它更容易,而且我个人认为这是用户所期望的。 its a paradigm used across numerous software applications.它是跨众多软件应用程序使用的范例。 if i touch a button, do i really want it to disappear, why has it disappeared, is it broken??如果我触摸一个按钮,我真的希望它消失吗,为什么它消失了,它坏了吗? if it is disabled and the user can see that something is happening (activity indicator, alert box -maybe overkill?) then they will be more confident that your app behaves in a predictable and reliable way如果它被禁用并且用户可以看到正在发生的事情(活动指示器,警报框 - 可能是矫枉过正?)那么他们将更有信心您的应用程序以可预测和可靠的方式运行

myButton.isEnabled = NO;

// set activity indicator while you're doing your stuff // 在你做你的事情时设置活动指示器

// later when completed // 稍后完成时

myButton.isEnabled = YES;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM