Display effect: (Uidatepicker & Uitoolbar & Uitextfield together with)
Excuse me: What are the steps to achieve?
First, after the program view is loaded, first create a date selector and set the date selector as the input view for the text box. When you are finished, create a toolbar that sets the background color and frame. Finally, set the toolbar to the secondary input view of the text box. Special NOTE: If you want to set a toolbar as a secondary input view for a text box, the X in frame | y | Width is invalid. Only height can customize settings.
The second step is to create the Cancel & Spring & Finish button in the toolbar, and note that all content in the toolbar must be a Uibarbuttonitem type of control. Add the Uibarbuttonitem created above to the toolbar when you are finished creating.
The third step, click the Cancel button, only need to exit the keyboard. There are a few things you need to do to click the Finish button.
L 1> Gets the date selected by the user. Because the date control is a local variable that we previously created purely code after the view was loaded, you cannot use Iboutlet to connect. So we need to define a global property of type Uidatepicker in the class extension to record. Note: We need to use the date format class to convert the Date object to a string object for display.
L 2> Set the conversion completion string to the text box.
L 3> Call the keyboard back.
Fourth step, how to automatically set the content of the date selector into the text input box? Since Uidatepicker is inherited from Uicontrol. Therefore Uidatepicker can use the Addtarget method to register a value change event.
Localized settings
The first step of code implementation
------------------------------VIEWCONTROLLER.M------------------------------
@interface Viewcontroller ()
/** Text Input Box */
@property (Weak, nonatomic) Iboutlet Uitextfield *textfield;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
1. Create a DatePicker
Uidatepicker *datepicker = [[Uidatepicker alloc] init];
1.2 Modifying the Display language
Datepicker.locale = [[Nslocale alloc] initwithlocaleidentifier:@ "Zh-hans"];
1.3 Changing the display mode
Datepicker.datepickermode = uidatepickermodedate;
2. Set text box pop-up DatePicker keyboard
Self.textField.inputView = DatePicker;
3. Create a toolbar
Uitoolbar *toolbar = [[Uitoolbar alloc] init];
3.2 Setting the background color of the toolbar
Toolbar.bartintcolor = [Uicolor Purplecolor];
3.3 Setting the frame of the tool bar
Toolbar.frame = CGRectMake (0, 0, 0, 44);
4. The Settings toolbar appears above the keyboard
Self.textField.inputAccessoryView = ToolBar;
}
@end
The second step of code implementation
------------------------------VIEWCONTROLLER.M------------------------------
4. Create a button in a toolbar
4.1 "Cancel" button
Uibarbuttonitem *itemcancel = [[Uibarbuttonitem alloc] initwithtitle:@ "Cancel" Style:uibarbuttonitemstyleplain target: Self action: @selector (Didclickcancelbutton)];
4.2 "Spring" button
Uibarbuttonitem *itemspring = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem: Uibarbuttonsystemitemflexiblespace Target:nil Action:nil];
4.3 "Finish" button
Uibarbuttonitem *itemdone = [[Uibarbuttonitem alloc] initwithtitle:@ "Done" Style:uibarbuttonitemstyledone target:self Action: @selector (Didclickdonebutton)];
4.4 Add the Uibarbuttonitem you created above to the toolbar
Toolbar.items = @[itemcancel, itemspring, Itemdone];
The third step of code implementation
------------------------------VIEWCONTROLLER.M------------------------------
/** used to record the creation of the date selector */
@property (nonatomic, weak) Uidatepicker *datepicker;
1.4 Record Date Picker
Self.datepicker = DatePicker;
#pragma mark-item Click event Handling
Click on the "Cancel" button
-(void) Didclickcancelbutton
{
Exit keyboard
[Self.view Endediting:yes];
}
Click on the "Finish" button
-(void) Didclickdonebutton
{
1. Gets the date selected by the user
NSDate *date = self.datePicker.date;
1.2 Using NSDateFormatter to convert a NSDate object to a NSString object
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
1.3 Setting the future string format
Formatter.dateformat = @ "YYYY-MM-DD";
1.4 Start Conversion
NSString *strdate = [Formatter stringfromdate:date];
2. Set a date to a text input box
Self.textField.text = strdate;
3. Exit keyboard
[Self.view Endediting:yes];
}
Fourth Step Code implementation
------------------------------VIEWCONTROLLER.M------------------------------
1.5 Registering a "Value change" Change event for DatePicker
[DatePicker addtarget:self Action: @selector (datechanged:) forcontrolevents:uicontroleventvaluechanged];
#pragma mark-click event for date selector
-(void) datechanged: (Uidatepicker *) DatePicker
{
1. Convert dates to strings using date formatting
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
Formatter.dateformat = @ "Yyyy-mm-dd HH:mm:ss";
NSString *str = [Formatter stringFromDate:datePicker.date];
NSLog (@ "Date:%@", str);
}
iOS Development--date selection