如何在按钮单击的UILabel中获取UIDatePicker值?

时间:2010-02-09 18:07:07

标签: iphone objective-c

请帮助我在iPhone应用程序的按钮点击时获得UIDatePickerUILabel的值。

3 个答案:

答案 0 :(得分:10)

创建一个插座,然后只询问其值

-(IBAction)OnButtonClick:(id)sender
{
   NSDate *date = datePicker.date;
   label.text = [date description];
}

答案 1 :(得分:0)

 DatePicker=[[UIDatePicker alloc]init];

    [DatePicker setDatePickerMode:UIDatePickerModeDate];

    //[DatePicker setMaximumDate:[NSDate date]];

    [DatePicker setMinimumDate:[NSDate date]];

    [DatePicker addTarget:self action:@selector(updateDate:) 

forControlEvents:UIControlEventValueChanged];



-(void) doneButton

{

  [self updateDate:DatePicker];    //calling this fuction on inside the button

}




-(void)updateDate:(UIDatePicker *)sender

{
    NSDate *date = self.DatePicker.date;

    _dateFormat = [[NSDateFormatter alloc]init];

    [_dateFormat setDateFormat:@"MM/dd/yyyy"];

    DateTextField.text = [_dateFormat stringFromDate:date];
}

答案 2 :(得分:0)

Swift 2.0

@IBAction func buttonPressed(sender: AnyObject) {
    let date: NSDate = timePicker.date
    label.text = date.description
}