Xcode/iOS5:按下 UITextField 时显示 UIDatePicker(+ 隐藏默认键盘)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8104414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode/iOS5: Show UIDatePicker when UITextField is pressed (+ hide default keyboard)
提问by filou
I'd like to call UIDatePicker only when UITextField is active. When the date is picked, it should be shown in the same UITextField.
我只想在 UITextField 处于活动状态时调用 UIDatePicker。选择日期后,它应该显示在同一个 UITextField 中。
I even don't know how to call the Picker.
我什至不知道如何调用 Picker。
Can anyone help me?
谁能帮我?
回答by Tendulkar
there is a delegate method called
有一个委托方法叫做
-(void)textFieldDidBeginEditing:(UITextField*)textField
{
[textField resignFirstResponder];
in this you have to do this
pick = [[UIDatePicker alloc] init];
[pick setFrame:CGRectMake(0,200,320,120)];
[pick addTarget:self action:@selector(done) forControlEvents:UIControlEventValueChanged]
[self.view addSubview:pick];
}
in
在
-(void)done
{
[textfield setText:pick.date];
}
release the pick in dealloc
在 dealloc 中释放选择
回答by jbat100
To add to Hitman's answer, I think you should implement the
添加到杀手的答案,我认为你应该实施
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
instead and return NO. Then show the date picker and fill the UITextField text according to the picker selection.
而是返回 NO。然后显示日期选择器并根据选择器选择填充 UITextField 文本。