IOS 点击TextField弹出DatePicker
时间:10月27日
用textfield的inputview和inputAccessoryView两个属性。创建datePicker,赋值给两个textfield的inputview属性。创建toolbar,包含一个Done按钮,赋值给inputAccessoryView属性。你需要用这个Done来退出inputview。
Done的事件处理:
if ( [textField isFirstResponder] ) {
????[textField resignFirstResponder];
}
Example
@interface CustomKeyboardAppDelegate : NSObject <</span>UIApplicationDelegate> {
...
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (nonatomic, retain) IBOutlet UIToolbar *toolbarInput;
@property (nonatomic, retain) IBOutlet UIDatePicker *datePickInput;
- (IBAction)dateChanged:(id)sender;
- (IBAction)doneEditing:(id)sender;
@end
在XIB文件中,拖出 一个UIToolbar和一个UIDatePicker,但不要附加到View中(拖到视图外面)。适当的连接Outlets。dateChanged:响应datepicker的ValueChanges,doneEditing:被ToolBar中的Done按钮点击时调用(Connection->Sent Actions->selectors)。以下是实现:
@implementation CustomKeyboardAppDelegate
@synthesize window=_window;
@synthesize textField = _textField;
@synthesize toolbarInput = _toolbarInput;
@synthesize datePickInput = _datePickInput;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
????self.textField.inputView = self.datePickInput;
????self.textField.inputAccessoryView = self.toolbarInput;
????... ???
}
...
- (IBAction)dateChanged:(id)sender {
????UIDatePicker *picker = (UIDatePicker *)sender;
????self.textField.text = [NSString stringWithFormat:@"%@", picker.date];
}
- (IBAction)doneEditing:(id)sender {
????[self.textField resignFirstResponder];
}
@end