IOS Uidatepicker+uitoolbar Time Picker Instance

Source: Internet
Author: User
Tags dateformat


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

Related Article

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.