I am trying to figure out how to use performSelector: afterDelay: to create a 5 second pause before a button executes its function. I started from a version with no delay:
-(IBAction)immediateActivate:(id)sender {
switch1.on = YES;
}
It worked great. Hit the button, switch turns on.
Then I did the following to create a delay:
-(IBAction)delayedActivate:(id)sender
{
[self performSelector:@selector(immediateActivate) withObject:nil afterDelay: 5.0];
}
-(void)immediate开发者_开发技巧Activate:(id)sender {
switch1.on = YES;
}
I changed the hook up in Interface Builder so the button now triggers delayedActivate. It builds without error but when I press my button I still get instantaneous activation. Can anyone see what I'm doing wrong please?
Thanks,
Dessie.
I'm guessing that although you say "I changed the hook up in Interface Builder", you did not - you are still hooked from the button to immediateActivate. You may also be hooked to delayedActivate, but it's possible to be hooked to more than one action. To confirm this, if you don't quite grasp how to use the connections inspector in IB, the simplest thing is just to add an NSLog to each of your methods, so that you can watch the Console to see when each method is called. - Hey, someone else gave the same answer while I was typing this! :)
Sounds like you might have the button connected to both actions, check in Interface Builder. Right-click the button, and expand the connections under 'Actions'.
If this is the case, you'll see the immediateActivate: fire first, then delatedActivate: fire 5 seconds later. You can check this using a couple of breakpoints or NSLog(@"Here!");
statements.
check by debugger what is the flow means -(IBAction)immediateActivate:(id)sender
calls first then immediateActivate
or immediateActivate
calls first.
Hopefully you are loosing in IBConnection.
Just a sidenote that there seems to be a bug in the debugger. I'm using Xcode 4.3 though this happened on 4.2.1 as well. If you create a breakpoint right after a performSelector:
call with a delay, the delay will be measured even when the app is paused on the breakpoint and the selector will execute immediately if the delay time has been reached during this paused time. In normal code execution however, all works fine.
精彩评论