The simple time selector, in fact, is to take advantage of the Uitextfield Inputview attribute. Look directly at the code:
#import "ViewController.h"
@interface Viewcontroller () <uitextfielddelegate>{
Uitextfield *textfiled;
NSString *timestr;
}
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
textfiled = [[Uitextfield alloc] Initwithframe:cgrectmake (70, 80, 120, 30)];
TextFiled.layer.borderColor = [[Uicolor Graycolor]cgcolor];
TextFiled.layer.borderWidth = 1.0f;
[Self.view addsubview:textfiled];
Add a time picker
Uidatepicker *datepicker = [[Uidatepicker alloc]init];
Settings Show only Chinese
[DatePicker setlocale:[nslocale localewithlocaleidentifier:@ "ZH-CN"]];
Add a scrolling event
[DatePicker addtarget:self Action: @selector (datechanged:) forcontrolevents:uicontroleventvaluechanged];
Set display date only
Datepicker.datepickermode = uidatepickermodedate;
When the cursor is moved to the text box, call the time picker
Textfiled.inputview = DatePicker;
Create a tool bar
Uitoolbar *toolbar = [[Uitoolbar alloc]init];
To set the color of a tool bar
Toolbar.bartintcolor = [Uicolor blackcolor];
Set the frame of a tool bar
Toolbar.frame = CGRectMake (0, 0, [UIScreen mainscreen].bounds.size.width, 35);
To add a button to a tool bar
Uibarbuttonitem *item0 = [[Uibarbuttonitem alloc]initwithtitle:nil Style:uibarbuttonitemstyleplain target:self Action : nil];
Uibarbuttonitem *item1 = [[Uibarbuttonitem alloc]initwithtitle:nil Style:uibarbuttonitemstyleplain target:self Action : nil];
Uibarbuttonitem *item2 = [[Uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace Target:nil Action:nil];
Uibarbuttonitem *item3 = [[Uibarbuttonitem alloc]initwithtitle:@ ' completed ' Style:uibarbuttonitemstyleplain target:self Action: @selector (click:)];
Toolbar.items = @[item0,item1,item2,item3];
Set up a secondary view of the Text entry box keyboard
Textfiled.inputaccessoryview = toolbar;
}
#pragma mark-Time selection
-(void) datechanged: (Uidatepicker *) sender{
NSDate *date = sender.date;
NSDateFormatter *yeardateformatter = [NSDateFormatter new];
Yeardateformatter.dateformat = @ "yyyy";
NSDateFormatter *monthdateformatter = [NSDateFormatter new];
Monthdateformatter.dateformat = @ "MM";
NSDateFormatter *daydateformatter=[nsdateformatter New];
[Daydateformatter setdateformat:@ "DD"];
TIMESTR = [NSString stringwithformat:@ "%@-%@-%@", [Yeardateformatter Stringfromdate:date],[monthdateformatter Stringfromdate:date],[daydateformatter Stringfromdate:date]];
}
#pragma mark-OK button click
-(void) Click: (UIButton *) sender
{
NSLog (@ "timestr>=%@", timestr);
Textfiled.text = Timestr;
[Textfiled Resignfirstresponder];
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any of the can is recreated.
}
@end