简体   繁体   中英

delay in hiding navigation right bar button item

I have a UITableview with a navigation bar on the top. I have a refresh button as the 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. 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.

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. 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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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