Srikar Appalaraju
Srikar Appalaraju

Reputation: 73668

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;
}

Upvotes: 0

Views: 593

Answers (2)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

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.

Upvotes: 1

Domestic Cat
Domestic Cat

Reputation: 1434

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.

Upvotes: 1

Related Questions