UIDatePicker DidSelectRow Method?

后端 未结 3 1705
栀梦
栀梦 2021-01-12 05:53

I know UIPickerViews use a didSelectRow method to update labels etc when a row is selected.

I have a UIDatePicker that updates a label when a row is selected and don

3条回答
  •  隐瞒了意图╮
    2021-01-12 06:09

    UIDatePicker is a UIControl, so use

    [picker addTarget:yourController action:@selector(yourAction:) forControlEvents:UIControlEventValueChanged];
    

    to send a message to your controller. The controller can then get the state of the date picker and update the label.

    If your picker and your label are both outlets in the same class, in viewDidLoad:

    [self.picker addTarget:self action:@selector(updateLabelFromPicker:) forControlEvents:UIControlEventValueChanged];
    

    Then create updateLabelFromPicker:

    - (IBAction)updateLabelFromPicker:(id)sender {
        self.label.text = [self.dateFormatter stringFromDate:self.picker.date];    
    }
    

    You'll want an NSDateFormatter to make the date pretty.

提交回复
热议问题