ios - How to show uidatepicker from uitableviewcell -
how can show uidatepicker
uitableviewcell, , have datepicker
have toolbar @ top lefts resign it? there tutorials on how this?
i've had exact same thing.
- insert
uitextfield
uitableviewcell
(you may need create customuitableviewcell
depending whether want appear on every dynamic cell ofuitableview
or on single static cell). create properties
uidatepicker
,uitoolbar
, ,uitextfield
, in ib hookuitextfield
propertyuitextfield
created in step 1 (if you're using customuitableviewcell
class, that'suitextfield
property need go):@property (strong, nonatomic) uidatepicker * datepicker; @property (strong, nonatomic) uitoolbar * datepickertoolbar; @property (strong, nonatomic) iboutlet uitextfield *textfield; ... @synthesize datepicker, datepickertoolbar, textfield;
setup
uidatepicker
,uitoolbar
, ,uitextfield
:- (void)viewdidload { // initialise uidatepicker datepicker = [[uidatepicker alloc] init]; datepicker.datepickermode = uidatepickermodetime; [datepicker addtarget:self action:@selector(datepickervaluechanged:) forcontrolevents:uicontroleventvaluechanged]; // method respond changes in picker value // setup uitoolbar uidatepicker datepickertoolbar = [[uitoolbar alloc] initwithframe:cgrectmake(0, 0, self.view.bounds.size.width, 44)]; [datepickertoolbar setbarstyle:uibarstyleblacktranslucent]; uibarbuttonitem *extraspace = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace target:nil action:nil]; uibarbuttonitem *donebutton = [[uibarbuttonitem alloc] initwithtitle:@"done" style:uibarbuttonitemstyledone target:self action:@selector(dismisspicker:)]; // method dismiss picker when "done" button pressed [datepickertoolbar setitems:[[nsarray alloc] initwithobjects: extraspace, donebutton, nil]]; // note: if you're using dynamic cells, below 2 lines need in cellforrowatindexpath method instead. // set uitextfield's inputview uidatepicker textfield.inputview = datepicker; // set uitextfield's inputaccessoryview uitoolbar textfield.inputaccessoryview = datepickertoolbar; }
setup
dismisspicker
method:-(void)dismisspicker:(id)sender{ [textfield resignfirstresponder]; }
setup
datepickervaluechanged
method:- (void)datepickervaluechanged:(id)sender{ nsdate *selecteddate = datepicker.date; nsdateformatter *df = [[nsdateformatter alloc] init]; [df setdateformat:@"hh:mm"]; [textfield settext:[df stringfromdate:selecteddate]]; }
note: in code, needed interface display time, hence date format set here (hh:mm
). because of this, notice in uidatepicker
initialization code in viewdidload
, i've set mode uidatepickermodetime
. date selections, may want set uidatepickermodedate
.
Comments
Post a Comment