Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

datepicker - iPhone display date picker on UITextField touch

I followed this thread: datepicker by clicking on textfield

I imported both of the following protocols:

@interface ViewController : UIViewController<UIActionSheetDelegate, UITextFieldDelegate> {

Then, in the implementation, I use the following:

- (void)viewDidLoad {

    textField.delegate = self;

[super viewDidLoad] }

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];  
}  

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

enter image description here

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use the UITextField 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 :)

EDIT: code

You fist hook up your textField with IB

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

In the implementation

UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
if (@available(iOS 13.4, *)) {
    [datePicker setPreferredDatePickerStyle:UIDatePickerStyleWheels];
}       
[self.myTextField setInputView: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];

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!

  • You could use a NSDateFormatter to improve the output of the string.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...