问题
I want to delay between each output result for say, 1 second. The very last line of the following code is throwing an error "Use of undeclared identifier 'dealCard'", but I have declared it in the header file as shown below.
- (IBAction)startPause:(id)sender
{
if ([self.deal length]>cardNum) {
timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(dealCard) userInfo:nil repeats:NO];
card.text = [NSString stringWithFormat:@"%i",cardNum];
[timer fire];
}
}
- (void) dealCard{
NSLog(@"dealCard: %d: ", cardNum+self.randCut);
cardNum=cardNum + 1;
[self startPause:(id)dealCard];
}
header file is next.
- (IBAction) startPause:(id)sender ;
- (void) dealCard;
回答1:
This is just a compilation error because dealCard is not a variable in the dealCard method. You can pass a selector as an argument. That would be done like:
[self startPause:@selector(dealCard)];
or even
[self startPause:_cmd];
since _cmd gives you the current selector and you happen to be in dealCard. Note however that usually the "sender" parameter is not used for passing a selector, but an object. For example an instance of UIButton sending a message like:
- (IBAction)myButtonResponse:(id)sender;
would pass self for the sender parameter.
回答2:
You can call your IBAction method with
[self startPause:nil];
since the sender parameter is never used. Remember that dealCard is a method, which has a selector associated. It's not a property (not even an object), so you can't pass it as an argument using only its name.
You have a recursive problem, too. dealCard calls startPause:, and startPause: calls dealCard again. You should have a stop condition in dealCard to end the "two level" recursive calls.
来源:https://stackoverflow.com/questions/17034993/can-an-ibaction-method-be-sent-a-message