gpt4 book ai didi

iphone - UIDatePicker DidSelectRow 方法?

转载 作者:太空狗 更新时间:2023-10-30 03:30:21 27 4
gpt4 key购买 nike

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

我有一个 UIDatePicker,它在选择一行并按下完成按钮时更新标签,但我还想要一个随每次选择而更新的 UILabel,因此它会随着用户更改日期而实时更新。我找不到日期选择器的任何方法,例如 didSelectRow。这怎么可能?

- (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,所以使用

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

向您的 Controller 发送消息。然后 Controller 可以获取日期选择器的状态并更新标签。

如果你的 picker 和 label 都是同一个类中的 outlet,在 viewDidLoad 中:

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

然后创建updateLabelFromPicker:

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

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

关于iphone - UIDatePicker DidSelectRow 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5748752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com