Scrungepipes
Scrungepipes

Reputation: 37581

cancelPreviousPerformRequestsWithTarget: isn't working for me

I have a requirement that the navigation bar be hidden by default. It will appear if the user taps on the screen, if the user taps a 2nd time then it will disappear, or if the user doesn't tap a 2nd time then it will disappear after 3 seconds.

So I implemented methods to handle this like this:

- (void) navigationBarDisplay
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(navigationBarHide) object:nil]; 
    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    [self performSelector:@selector(hideNavigationBar) withObject:self afterDelay:3.0];
}


- (void) navigationBarHide
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(navigationBarHide) object:nil]; 
    [[self navigationController] setNavigationBarHidden:YES animated:YES];
}

I call cancelPreviousPerformRequestWithTarget at the start of each function to cancel any previous outstanding call to performSelector:@selector(hideNavigationBar) and thus to reset things so the delay of 3 will always apply after the navigation bar is displayed.

However if I tap the screen to make the navigation bar appear, then before 3 seconds has expired I tap it again to make it disappear, then tap it a 3rd time to make it reappear, then it is being auto-hidden 3 seconds after the 1st tap, and not 3 seconds after the 3rd tap.

I also tried with

[[NSRunLoop mainRunLoop ]cancelPerformSelector:@selector(navigationBarHide) target:self argument:nil]; 

But its the same.

Any ideas why this isn't working? Or if there's a better solution?

Upvotes: 0

Views: 2260

Answers (1)

David Gelhar
David Gelhar

Reputation: 27900

Looks to me like your 'cancel' doesn't match the 'perform':

NSObject cancelPreviousPerformRequestsWithTarget:self 
selector:@selector(hideNavigationBar) object:nil

vs.

[self performSelector:@selector(hideNavigationBar) withObject:self afterDelay:3.0];

in one case 'object' is nil; in the other it's self.

Upvotes: 5

Related Questions