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.

  1. insert uitextfield uitableviewcell (you may need create custom uitableviewcell depending whether want appear on every dynamic cell of uitableview or on single static cell).
  2. create properties uidatepicker, uitoolbar, , uitextfield, in ib hook uitextfield property uitextfield created in step 1 (if you're using custom uitableviewcell class, that's uitextfield property need go):

    @property (strong, nonatomic) uidatepicker * datepicker; @property (strong, nonatomic) uitoolbar * datepickertoolbar; @property (strong, nonatomic) iboutlet uitextfield *textfield;  ...  @synthesize datepicker, datepickertoolbar, textfield; 
  3. 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; } 
  4. setup dismisspicker method:

    -(void)dismisspicker:(id)sender{     [textfield resignfirstresponder]; } 
  5. 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

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -