NSTImer & sending args throught selector
I am trying to do animation. At some point I want to execute a method through NSTimer
. I need to pass in 1 argument. This I do it through userInfo
in NSTimer
. In the selector method I am trying to access back this passed arg. But when I actually run this I get the following exception. It says invalid argument. What am I doing wrong?
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FLOViewController hideCellView]: unrecognized selector sent to instance 0x1681d0'"
-(void)hideCellView:(NSTimer *)timer
{
UITableViewCell *cellView = (UITableViewCell *)[timer userInfo];
[cellView addSubview:self.extrasView];
return;
}
-(IBAction)showExtras:(id)sender
{
if(![sender isKindOfClass: [UIButton class]]) return; // be paranoid
self.searchResTable.scrollEnabled = NO;
//Get the exact cell where the click happened
UIButton *button = sender;
CGPoint correctedPoint = [button convertPoint:button.bounds.origin toView:self.searchResTable];
NSIndexPath *indexPath = [self.searchResTable indexPathForRowAtPoint:correctedPoint];
UITableViewCell *cellView = [self.searchResTable cellForRowAtIndexPath:indexPath];
//now run animation
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:cellView
cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:1];
[NSTimer scheduledTimerWithTimeInterval:0.5
开发者_如何学编程 target:self
selector:@selector(hideCellView)
userInfo:cellView
repeats:NO];
[UIView commitAnimations];
return;
}
You have passed the wrong selector.
selector:@selector(hideCellView)
It should be,
selector:@selector(hideCellView:)
That said you should consider sending the index path of the table view cell rather than the cell itself as cells are reused. You can get the cell later using cellForRowAtIndexPath:
method the table view in the hideCellView:
method.
If you are looking to set a delay in the animation then use the setAnimationDelay:
method rather that using NSTimer
.
The quick answer is to replace the [NSTimer scheduledTimerWithTimeInterval ...
line with:
[self performSelector:@selector(hideCellView:) withObject:cellView afterDelay:0.5]
But this is not the correct way to handle running code after an animation. Check out blocks based animation using something like animateWithDuration:animations:completion:
. Apple docs are a good place to start.
精彩评论