iPhone显示日期选择器在UITextField触摸 [英] iPhone display date picker on UITextField touch

查看:131
本文介绍了iPhone显示日期选择器在UITextField触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照这个线程:通过点击文本字段的日期戳

I followed this thread: datepicker by clicking on textfield

我导入了以下协议:

@interface ViewController : UIViewController<UIActionSheetDelegate, UITextFieldDelegate> {

然后,在实现中,我使用以下内容:

Then, in the implementation, I use the following:

- (void)viewDidLoad {

    textField.delegate = self;

[super viewDidLoad] }

最后,我把实际代码显示日期选择器(从线程)。我也将其全部连接到IB。

Lastly, I put the actual code to display the date picker (from the thread). I also linked it all up in the IB.

    //Date Picker
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{    
    [aTextField resignFirstResponder];  

    pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];  

    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];  
    pickerView.datePickerMode = UIDatePickerModeDate;  
    pickerView.hidden = NO;  
    pickerView.date = [NSDate date];  

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
    pickerToolbar.barStyle = UIBarStyleBlackOpaque;  
    [pickerToolbar sizeToFit];  

    NSMutableArray *barItems = [[NSMutableArray alloc] init];  

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
    [barItems addObject:flexSpace];  

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];  
    [barItems addObject:doneBtn];  

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];  
    [barItems addObject:cancelBtn];  

    [pickerToolbar setItems:barItems animated:YES];  

    [pickerViewPopup addSubview:pickerToolbar];  
    [pickerViewPopup addSubview:pickerView];  
    [pickerViewPopup showInView:self.view];  
    [pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];    
}  

-(void)doneButtonPressed:(id)sender{  
    //Do something here here with the value selected using [pickerView date] to get that value  
        [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];  
}  

-(void)cancelButtonPressed:(id)sender{  
    [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];  
}  

然而,当我点击UITextField时,我得到这个:

However, when I click on the UITextField, I get this:

我做错了什么?

推荐答案

您可以使用UITextView输入视图属性。只需在代码中分配一个常规的UIDatePicker,并将其分配给textfield的inputView。这样做会显示UIPickerView而不是键盘。如果您需要任何代码,请随时询问:)

You could use the UITextView input view property. Just alloc init a regular UIDatePicker in code and assign it to the textfield's inputView. Doing this, the UIPickerView will be presented instead of the keyboard. If you need any code feel free to ask :)

编辑:代码

你的拳头挂钩你的textField与IB

You fist hook up your textField with IB

@interface myViewController <UITextFieldDelegate>
@property (nonatomic,weak) IBOutlet UITextField *myTextField;
@end 

在实现中

UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];       
[self.myTextField setInputView:datePicker];

当您点击textField时,会显示一个UIDatePicker。注意我添加到datePicker的操作。

That will bring up a UIDatePicker when you tap on the textField. Note the action that I added to the datePicker.

[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];

要在每次用户在选择器中滚动时更新textField,您只需要实现updateTextField方法在viewController中的某个地方

To update the textField each time that the user scrolls in the picker, you just need to implement the updateTextField method somewhere in the viewController

-(void)updateTextField:(id)sender
{
  UIDatePicker *picker = (UIDatePicker*)self.myTextField.inputView;
  self.myTextField.text = [NSString stringWithFormat:@"%@",picker.date];
}

使用最后一种方法,每次用户旋转选择器时,文本框将更新!

With this last method, every time the user rotates the picker, the textfield will update!


  • 您可以使用NSDateFormatter来改善字符串的输出。

这篇关于iPhone显示日期选择器在UITextField触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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