Code Explanation: (Additional code is explained later)
Home design Uipickerview Style design:
Leftarray = ["Flower", "color", "shape"];
Let array1 = ["Jasmine", "Rose", "Tulip", "Violet"];
Let array2 = ["Red", "yellow", "black", "white", "purple"];
Let array3 = ["Round", "Square", "oval"];
Righarray = [Array1,array2,array3];
Let Pickerv = Uipickerview.init (Frame:cgrectmake (0, Self.view.frame.size.width, 260));
Pickerv.backgroundcolor = Uicolor.browncolor ();
Sets whether the selected state is displayed
Pickerv.showsselectionindicator = true;
Set up Proxy
Pickerv.datasource = self;
Pickerv.delegate = self;
Self.view.addSubview (Pickerv);
Let btn = Uibutton.init (Frame:cgrectmake (+ 380, f_device_w () -40*2, 40));
Btn.settitle ("Time Picker", forState:UIControlState.Normal);
Btn.backgroundcolor = Uicolor.cyancolor ();
Btn.layer.cornerRadius = 5;
Self.view.addSubview (BTN);
Btn.addtarget (Self, Action: #selector (Btnclick), forcontrolevents:. Touchupinside);
To add an agent, perform the necessary proxy methods:
MARK:---The Uipickerviewdatasource method that must be implemented
return several partitions
Func Numberofcomponentsinpickerview (Pickerview:uipickerview), Int {
return 2;
}
Returns the number of partition rows
Func Pickerview (Pickerview:uipickerview, Numberofrowsincomponent component:int), Int {
if (component = = 0)
{
return leftarray.count;
}
Else
{
Gets which row of the No. 0 partition is selected
Let Row1 = pickerview.selectedrowincomponent (0);
The data corresponding to the 1th partition is then obtained based on the obtained line number.
Let arrary = Righarray.objectatindex (ROW1);
return arrary.count;
}
}
Time Picker Uidatepicker
Uidatepicker time picker, inherited from Uicontrol
Let Datepick = Uidatepicker.init (frame:cgrectmake (0, N, self.view.frame.size.width, 300));
Set display time style, Chinese or English, default English display style
Datepick.locale = Nslocale.init (localeidentifier: "ZH_CN");//Chinese
Datepick.backgroundcolor = Uicolor.lightgraycolor ();
Set Display Type
Datepick.datepickermode = uidatepickermode.dateandtime;//Date and time
Set the default date
Datepick.date = Nsdate.init ();
Set the minimum date that can be selected
Datepick.minimumdate = Nsdate.init (timeintervalsincenow:-(60*60*24*7));
Set the maximum date that can be selected
Datepick.maximumdate = Nsdate.init (timeintervalsincenow:60*60*24*7);
Add a method to get the displayed date value
Datepick.addtarget (Self, Action: #selector (Changevaluedate), forcontrolevents:. valuechanged);
Self.view.addSubview (Datepick);
Datelabel = Uilabel.init (Frame:cgrectmake (+ 390, self.view.frame.size.width-80, 50));
Datelabel.textcolor = Uicolor.bluecolor ();
Datelabel.textalignment = Nstextalignment.center;
Self.view.addSubview (Datelabel);
}
Func changevaluedate (datepickerv:uidatepicker) {
Gets the currently selected time
Let datee = datepickerv.date;
Convert time format
Let Formatterr = Nsdateformatter.init ();
Formatterr.dateformat = "Yyyy-mm-dd hh:mm:ss";
Let Datestr = Formatterr.stringfromdate (datee);
Datelabel.text = Datestr;
}
Additional Code Explanation:
//mark:---It is important to note that there is no macro definition define in swift, and the macro definition can be written as a Func method to invoke
such as: Get the width and height of the screen method
Func f_device_w (), CGFloat {
Return Uiscreen.mainscreen (). Bounds.size.width;
}
Func F_device_h (), CGFloat {
Return Uiscreen.mainscreen (). Bounds.size.height;
}
Set the RGBA macro method
Func RGBA (R:cgfloat, G:cgfloat, B:cgfloat, a:cgfloat), Uicolor {
Return Uicolor (red:r/255.0, green:g/255.0, blue:b/255.0, alpha:a);
}
Judging the system version method, using the method: such as: NavBar = UIView (frame:cgrectmake (0, 0, +/-Is_ios7 ()? 64:44))//ternary operation
Func Is_ios7 ()->bool {return (Uidevice.currentdevice (). Systemversion as NSString). Doublevalue >= 7.0;
}
Func Is_ios8 (), Bool {return (Uidevice.currentdevice (). Systemversion as NSString). Doublevalue >= 8.0;
}
The results show:
SOURCE Download demo:http://download.csdn.net/detail/hbblzjy/9592406
Swift base Pickerview (time) Selector