UIDatePicker DidSelectRow方法? [英] UIDatePicker DidSelectRow Method?

查看:97
本文介绍了UIDatePicker DidSelectRow方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道UIPickerViews在选择行时使用didSelectRow方法来更新标签等。

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

我有一个UIDatePicker在选择行时更新标签按钮,但我也想要一个UILabel更新每个选择,所以其实时更新为用户更改日期。我找不到任何方法,如didSelectRow的日期选择器虽然。这是怎么回事?

I have a UIDatePicker that updates a label when a row is selected and done button is pressed, but I also want a UILabel that updates with each selection, so its live updating as the user changes the date. I can't find any method such as didSelectRow for the date picker though. How is this possible?

- (IBAction)dateButtonPressed
{
    [dateView setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:dateView];

    UIBarButtonItem *acceptButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismssPickerChangeDate)];
    self.navigationItem.rightBarButtonItem = acceptButton;
    [acceptButton release];

    UIBarButtonItem *cancelButton1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismssPickerCancel)];
    self.navigationItem.leftBarButtonItem = cancelButton1;
    [cancelButton1 release];

    self.navigationItem.title = @"Select Date";
    self.navigationItem.hidesBackButton = YES;
    [UIView animateWithDuration:.5 animations:^{
        [dateView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }];
    NSDate *myDate = picker.date;

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    //NSDate *date = [NSDate date]; 
    [dateFormatter setDateFormat:@"MMM d y"]; 
    NSString *dateString = [dateFormatter stringFromDate:myDate]; 
    NSLog(@"New Date is = %@",dateString);
    dateLabel.text = dateString;
}


推荐答案

UIDatePicker是一个UIControl使用

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.

如果你的选择器和你的标签都是同一个类的视图,在viewDidLoad: p>

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

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

然后创建updateLabelFromPicker:

Then create updateLabelFromPicker:

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

你需要一个NSDateFormatter使日期漂亮。

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

这篇关于UIDatePicker DidSelectRow方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆