views:

922

answers:

3

I have a UIPickerView and I would like to be notified when the selectRow animation is done.

I tried the following approach in my view controller which has a reference to the UIPickerView and it won't work:

-(void)viewDidLoad { ...

[UIPickerView setAnimationDelegate:self];

[UIPickerView setAnimationDidStopSelector:@selector(animationFin ished:finished:context]; ...

}

  • (void)animationFinishedNSString *)animationID finishedBOOL)finished contextvoid *)context {

if (finished) {

}

}

Then somewhere in my code, I initiate the animation:

[picker selectRow:random() % pickerDataCount inComponent:0 animated:YES];

A: 

Hi, you need to nest the method call into a beginAnimations/commitAnimation block.

- (void) animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context {
    NSLog(@"Here I am");
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    [UIView beginAnimations:@"1" context:nil]; // nil = dummy
    [UIPickerView setAnimationDelegate:self];
    [UIPickerView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
    [myPickerView selectRow:0 inComponent:0 animated:YES]; // jump with any swipe in picker always to row=0 as dummy to initiate animation
    [UIView commitAnimations];

    //...whatever comes in addition...
}
filmore
A: 

This doesn't work for me. I get the callback instantly (before the animation finishes).

Kevin Lambert
This should be a comment.
rjstelling
A: 

you could post a notification to self from viewForRow when it asks view for component & row you are interested.

You just need to hold row & component as properties and set them before you call selectRow. And, in viewForRow

if ( (component == [self component] && (row == [self row] ) post a notification to self

HiQuLABS