iphone SDK:如何实现模态日期选择器? [英] iphone SDK: How to implement a modal date picker?

查看:109
本文介绍了iphone SDK:如何实现模态日期选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I implement a modal date picker?

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

What I want to happen is when the user enters a text field, a new view is created that has the date picker on it. From there, the user can interact and select a date. When the view is closed it should somehow return the date to it's caller. Is this possible?

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

BTW, I cannot put the date picker on my main form because of space.

在此先感谢。

推荐答案

使用代表!

标题:

@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

class:

@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。
当你想要实际使用它时:

All that crap in loadView can be replaced with a NIB if you prefer. Just make sure to declare datePicker as an IBOutlet. And when you want to actually use it:

- (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:如何实现模态日期选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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