How to get callback from UIPickerView when the sel

2019-05-26 14:15发布

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

3条回答
smile是对你的礼貌
2楼-- · 2019-05-26 14:25

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...
}
查看更多
Viruses.
3楼-- · 2019-05-26 14:25

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

查看更多
Bombasti
4楼-- · 2019-05-26 14:39

I solved it with a mix out of different Answers mentioned here. The behaviour will be that it will wait until the scrolling finished and then save the selected value.

  1. Create two variables, which store the scrolling state and the should save state. In the didSet you will check, if the the save button has been pressed while the picker is scrolling. If yes, call save after the picker has finished scrolling.

    var shouldSave = false
    var pickerViewIsScrolling = false {
        didSet {
            if !pickerViewIsScrolling && shouldSave {
                save()
            }
        }
    }
    
  2. To recognize if the picker is scrolling, add pickerViewIsScrolling = true in the viewForRow method of the picker.

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        pickerViewIsScrolling = true
        ...
    }
    
  3. To recognize if the picker has stopped scrolling add pickerViewIsScrolling = false to the didSelectRow of the picker.

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
         pickerViewIsScrolling = false 
         ...
    }
    
  4. In your save() function add the following to check wether the picker is scrolling (and save after it stopped) or is not scrolling and save directly.

    func save() {
         if(pickerViewIsScrolling){
              shouldSave = true
              return
         }
    
         // Save the Data...
    
    }
    
  5. And finally add to your viewDidAppear function this line to catch the pickerViewIsScrolling = true of the initial generated views.

    override func viewDidAppear(_ animated: Bool) {
    
         super.viewDidAppear(animated)
    
         pickerViewIsScrolling = false
    
         ...
    
    }
    

This works fine for me. I also implemented the deactivation of the button while save was pressed and it is waiting for the scrolling to finish. So the user won't be confused why nothing is happening until the scrolling stops.

查看更多
登录 后发表回答