gpt4 book ai didi

iPhone SDK : How to implement a modal date picker?

转载 作者:行者123 更新时间:2023-12-03 18:51:46 26 4
gpt4 key购买 nike

如何实现模态日期选择器?

我想要发生的是,当用户输入文本字段时,会创建一个带有日期选择器的新 View 。从那里,用户可以进行交互并选择日期。当 View 关闭时,它应该以某种方式将日期返回给调用者。这可能吗?

顺便说一句,由于空间原因,我无法将日期选择器放在主窗体上。

提前致谢。

最佳答案

使用委托(delegate)!

标题:

@class DatePickerController;
@protocol DatePickerControllerDelegate
- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date;
@end

@interface DatePickerController : UIViewController {
UIDatePicker *datePicker;
NSObject <DatePickerControllerDelegate> *delegate;
}

@property (nonatomic, retain) UIDatePicker *datePicker;
@property (nonatomic, assign) NSObject <DatePickerControllerDelegate> *delegate;
@end

类别:

@implementation DatePickerController
- (void) loadView {
self.view = [[[UIView alloc] init] autorelease];
self.datePicker = [[[UIDatePicker alloc] init] autorelease];
[self.view addSubview:self.datePicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Done" forState:UIControlStateNormal];
button.center = CGPointMake(160,240);
[button addTarget:self action:@selector(done) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}

- (void) done {
[delegate datePickerController:self didPickDate:datePicker.date];
}

- (void) dealloc {
[datePicker release];
[super dealloc];
}

@end

如果您愿意,loadView 中的所有废话都可以用 NIB 替换。只需确保将 datePicker 声明为 IBOutlet。当你想实际使用它时:

- (void) pickDate {
DatePickerController *screen = [[[DatePickerController alloc] init] autorelease];
screen.delegate = self;
[self presentModalViewController:screen animated:YES];
}

- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date {
[self doSomethingWithDate:date];
[controller dismissModalViewControllerAnimated:YES];
}

关于iPhone SDK : How to implement a modal date picker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1624866/

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