IOS development-display DatePicker when hitting textfield

Source: Internet
Author: User

Recently encountered a problem in the project. On the setting page, there are two input boxes. When you want to press the box, the date control is displayed, and you can select the date and time. Baidu once and finds that there is no complete solution. Now, let's share it. You can use the inputview and inputAccessoryView attributes of textfield. Create a datePicker and assign the inputview attribute to two textfields. Create a toolbar that contains a Done button and assign it to the inputAccessoryView attribute. You need to use this Done to exit the inputview. Done event processing: if ([textField1 isFirstResponder]) {[textField1 resignFirstResponder];} else if ([textField2 isFirstResponder]) {[textField2 resignFirstResponder];} Example @ interface reply: NSObject <UIApplicationDelegate> {... @ property (nonatomic, retain) IBOutlet UIWindow * window; @ property (nonatomic, retain) IBOutlet UITextField * textField; @ property (nonatomic, retain) IB Outlet UIToolbar * accessoryView; @ property (nonatomic, retain) IBOutlet UIDatePicker * customInput;-(IBAction) dateChanged :( id) sender;-(IBAction) doneEditing :( id) sender; @ end in the XIB file, drag a UIToolbar and a UIDatePicker, but do not attach it to the View (drag it out of the View ). Connect to Outlets as appropriate. DateChanged: responds to the ValueChanges and doneEditing of datepicker. It is called when the Done button in the ToolBar is clicked (Connection-> Sent Actions-> selectors ). Implementation: @ implementation CustomKeyboardAppDelegate @ synthesize window = _ window; @ synthesize textField = _ textField; @ synthesize accessoryView = _ accessoryView; @ synthesize customInput = _ customInput;-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. textField. inputView = self. customInput; self. textField. inputAccessoryView = self. accessoryView ;...}... -(IBAction) dateChanged :( id) sender {UIDatePicker * picker = (UIDatePicker *) sender; self. textField. text = [NSString stringWithFormat: @ "% @", picker. date];}-(IBAction) doneEditing :( id) sender {[self. textField resignFirstResponder];} @ end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.