iphone - How to make UIDatePicker to show user selected time -
i have uidatepicker letting user chose time . uitableview
in uiview
on click of uibutton
uiddatepicker
appears, when user choose time , press button dismiss uiddatepicker
view , if wants check whats time had chose in uiddatepicker
. uiddatepicker
doesn't show time user had selected shows current time or time start time of datepicker . i want display time user has chosen user when opens datepicker again. code when user press uibutton
display uiddatepicker
view
-(ibaction)settingsbuttonchanged:(uibutton *)sender { usertimepicker = [[uidatepicker alloc]init]; [usertimepicker addtarget: self action: @selector(usertimepickervaluechanged:)forcontrolevents: uicontroleventvaluechanged]; }
this code lets datepicker have custom time limit
-(ibaction)datepickertimenbuttonchanged:(uibutton *)control { if (control.tag == 1) { self.selectedbutton = control.tag; int starttime = 7; int endtime = 11; nsdate *date1 = [nsdate date]; components = [gregorian components: nsuintegermax fromdate: date1]; [components sethour:starttime];; [components setminute:00]; nsdate *startdate = [gregorian datefromcomponents:components]; [components sethour:endtime]; [components setminute:00]; nsdate *enddate = [gregorian datefromcomponents:components]; morningbutton.selected = yes; afternoonbutton.selected = no; eveningbutton.selected = no; nightbutton.selected = no; [usertimepicker setdatepickermode:uidatepickermodetime]; [usertimepicker setminimumdate:startdate]; [usertimepicker setmaximumdate:enddate]; [usertimepicker setdate:startdate animated:yes]; [usertimepicker reloadinputviews]; } if (control.tag == 2) { self.selectedbutton = control.tag; morningbutton.selected = no; afternoonbutton.selected = yes; eveningbutton.selected = no; nightbutton.selected = no; int starttime = 12; int endtime = 15; nsdate *date1 = [nsdate date]; components = [gregorian components: nsuintegermax fromdate: date1]; [components sethour:starttime]; [components setminute:00]; nsdate *startdate = [gregorian datefromcomponents:components]; [components sethour:endtime]; [components setminute:00]; nsdate *enddate = [gregorian datefromcomponents:components]; [usertimepicker setdatepickermode:uidatepickermodetime]; [usertimepicker setminimumdate:startdate]; [usertimepicker setmaximumdate:enddate]; [usertimepicker setdate:startdate animated:yes]; [usertimepicker reloadinputviews]; } }
code lets user selects own time
-(void) usertimepickervaluechanged:(uibutton *)sender { switch (self.selectedbutton) { case 1: { nsuserdefaults *userselectedmorningtime = [nsuserdefaults standarduserdefaults]; nsdate *selectedmorningtime = [usertimepicker date]; nslog (@"setting: morningtimekey"); storedmorntime = selectedmorningtime; [userselectedmorningtime setobject:selectedmorningtime forkey:@"morningtimekey"]; [userselectedmorningtime synchronize]; } break; case 2: { nsuserdefaults *userselectedafternoontime = [nsuserdefaults standarduserdefaults]; nsdate *selectedafternoontime = [usertimepicker date]; nslog (@"setting: afternoontimekey"); storedafternoontime = selectedafternoontime; [userselectedafternoontime setobject:selectedafternoontime forkey:@"afternoontimekey"]; [userselectedafternoontime synchronize]; } break; }
from make out code saving selected time nsuserdefaults
, never using saved time it.
my guess if change should work.
-(ibaction)datepickertimenbuttonchanged:(uibutton *)control { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; if (control.tag == 1) { self.selectedbutton = control.tag; int starttime = 7; int endtime = 11; //saved morning time date user defaults nsdate *date1 = [defaults objectforkey:@"morningtimekey"]; if(!date1) date1 = [nsdate date]; components = [gregorian components: nsuintegermax fromdate: date1]; [components sethour:starttime];; [components setminute:00]; nsdate *startdate = [gregorian datefromcomponents:components]; [components sethour:endtime]; [components setminute:00]; nsdate *enddate = [gregorian datefromcomponents:components]; morningbutton.selected = yes; afternoonbutton.selected = no; eveningbutton.selected = no; nightbutton.selected = no; [usertimepicker setdatepickermode:uidatepickermodetime]; [usertimepicker setminimumdate:startdate]; [usertimepicker setmaximumdate:enddate]; [usertimepicker setdate:startdate animated:yes]; [usertimepicker reloadinputviews]; } if (control.tag == 2) { self.selectedbutton = control.tag; morningbutton.selected = no; afternoonbutton.selected = yes; eveningbutton.selected = no; nightbutton.selected = no; int starttime = 12; int endtime = 15; //saved afternoon time date user defaults nsdate *date1 = [defaults objectforkey:@"afternoontimekey"]; if(!date1) date1 = [nsdate date]; components = [gregorian components: nsuintegermax fromdate: date1]; [components sethour:starttime]; [components setminute:00]; nsdate *startdate = [gregorian datefromcomponents:components]; [components sethour:endtime]; [components setminute:00]; nsdate *enddate = [gregorian datefromcomponents:components]; [usertimepicker setdatepickermode:uidatepickermodetime]; [usertimepicker setminimumdate:startdate]; [usertimepicker setmaximumdate:enddate]; [usertimepicker setdate:startdate animated:yes]; [usertimepicker reloadinputviews]; } }
Comments
Post a Comment