Creation of Uidatepicker
Uidatepicker is a control that can be used to select or set a date, but it is a control that is like a runner and is an apple-made control for the calendar, as shown in the following illustration:
In addition to the Uidatepicker control, there is a more versatile wheel-shaped control: Uipickerview, except that the Uidatepicker control displays the calendar, and the content displayed in the Uipickerview control needs to be set ourselves with code. This article briefly introduces the Uidatepicker control, and the next article will introduce Uipickerview.
1, run Xcode, create a new single View application, the name is Uidatepicker Test, and other settings as shown in the following figure:
2, click Viewcontroller.xib, Open Interface Builder. Drag a Uidatepicker control to the view:
3, and then drag a button on the view, and modify the button name to select:
When the button is clicked, an alert is displayed to display the user's selections.
4. Create mappings: Open Assistant Editor, select the Uidatepicker control, hold down controls, and drag to ViewController.h:
Create a new outlet with the name DatePicker:
Then create an action map for the button in the same way, the name is buttonpressed, and the event type is the default touch up Inside.
5, select the Uidatepicker control, open the attribute Inspector, in which set the maximum date for example, we set up here for 2100-12-31:
instance
and today we're going to do the time picker finished product specific effects are as follows:
We customize a lgdatepickerview and implement it in Lgdatepickerview.
Background translucent:
The background is translucent, and when clicked on the gray background, the time picker disappears. In the Lgdatepickerview initialization method, the code is as follows:
Copy Code code as follows:
-(ID) init
{
self = [super init];
if (self) {
Background translucent, binding cancellation method
Uicontrol *control = [[Uicontrol alloc] initwithframe:screen_bounds];
Control.backgroundcolor = [Uicolor colorwithred:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.5f];
[Self Addsubview:control];
[Control addtarget:self Action: @selector (Actioncancel:) forcontrolevents:uicontroleventtouchupinside];
}
return self;
}
Actioncancel Method of binding:
Copy Code code as follows:
-(void) Actioncancel: (ID) sender
{
[Self removefromsuperview];
}
OK Cancel button:
See above OK Cancel button, what would you do, write a uiview top put two UIButton. This can also be achieved. We can also use Uitoolbar. In the Lgdatepickerview initialization method, add the following section of code:
Copy Code code as follows:
Toolbar
Uitoolbar *toolbar = [[Uitoolbar alloc] Initwithframe:cgrectmake (0, screen.height-250, Screen.width, 50)];
Toolbar.autoresizingmask = Uiviewautoresizingflexibletopmargin | Uiviewautoresizingflexiblewidth;
Uibarbuttonitem *itemcanceldone = [[Uibarbuttonitem alloc] initwithtitle:@ "OK" style:uibarbuttonitemstyleplain target : Self action: @selector (actionconfirm:)];
Uibarbuttonitem *itemcancel = [[Uibarbuttonitem alloc] initwithtitle:@ ' Cancel ' style:uibarbuttonitemstyleplain target: Self action: @selector (Actioncancel:)];
Uibarbuttonitem *space = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace Target:nil Action:nil];
[Toolbar Setitems:[nsarray Arraywithobjects:itemcancel,space,itemcanceldone, Nil]];
[Control Addsubview:toolbar];
Actioncancel has been achieved. The following implements the Actionconfirm method. How does it work?
Click to get the time, and then through the agent agent out.
Time Picker disappears:
Copy Code code as follows:
-(void) Actionconfirm: (ID) sender
{
if ([Self.delegate respondstoselector: @selector (datepickerview:didselecttime:)]) {
[Self.delegate datepickerview:self didSelectTime:self.datePicker.date];
}
[Self removefromsuperview];
}
Proxy method:
In LGDatePickerView.h
Copy Code code as follows:
@protocol lgdatepickerviewdelegate <NSObject>
-(void) Datepickerview: (Lgdatepickerview *) DatePicker didselecttime: (NSDate *) time;
@end
Create Uidatepicker:
Define a global variable in LGDatePickerView.h
Copy Code code as follows:
@property (nonatomic, strong) Uidatepicker *datepicker;
In the Lgdatepickerview initialization method, add the following section of code:
Copy Code code as follows:
Uidatepicker *datepicker = [[Uidatepicker alloc] init];
Datepicker.backgroundcolor = [Uicolor Whitecolor];
Datepicker.datepickermode = Uidatepickermodecountdowntimer;
Datepicker.date = [NSDate Date];
Datepicker.frame = CGRectMake (0, screen.height-200, screen.width, 220);
[Control Addsubview:datepicker];
Self.datepicker = DatePicker;
Using Lgdatepickerview
It's easy to use, create it, and then load the Self.view:
Copy Code code as follows:
Lgdatepickerview *datepicker = [[Lgdatepickerview alloc] init];
Datepicker.delegate = self;
DatePicker.datePicker.date = [NSDate Date];
Datepicker.frame = Self.view.bounds;
[Self.view Addsubview:datepicker];