简体   繁体   English

ios 6:UITextField中的UIDatePicker

[英]ios 6: UIDatePicker in the UITextField

I have problems with connecting datepicker with text field. 我在将datepicker与文本字段连接时遇到问题。 I have tried all solution from stackoverflow but they don't work in ios 6. Can you desribe how to make it, I want to tap on text field and choose date via datepicker. 我已经尝试了stackoverflow的所有解决方案,但是它们在ios 6中不起作用。你能想到如何制作它,我想点击文本字段并通过datepicker选择日期。

You should use the inputView property of your UITextField to make the picker show in place of the keyboard. 您应该使用UITextFieldinputView属性来使选择器显示代替键盘。 iOS6 will take care of everything for you then (showing the picker with the animation instead of the keyboard, etc). iOS6将为您处理所有事情(使用动画而不是键盘显示选择器等)。

Note: You will probably want to add some inputAccessoryView too (generally an UIToolBar with some "OK" button in it) to make the user be able to dismiss the picker (the IBAction of your "OK" button will simply call [textField resignFirstResponder] for that to happen of course) as an UIDatePicker does not have any button to validate your input (whereas the keyboard has its "Return Key") 注意:您可能也想添加一些inputAccessoryView (通常是一个带有一些“OK”按钮的UIToolBar ),以使用户能够关闭选择器(“OK”按钮的IBAction将只调用[textField resignFirstResponder]当然,因为UIDatePicker没有任何按钮来验证你的输入(而键盘有“返回键”)

#import "TextfieldwithDatepickerViewController.h"

UIActionSheet *pickerViewPopup;

@implementation TextfieldwithDatepickerViewController
- (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];  
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM