iphone - I'm Trying to figure out the timing of the iOS Keyboard -
so here situation of timing. have uilabel want update every time keyboard updates uitextfield. have 2 uitextfields 1 ever first responder don't worry there being 2 have them end purposes. problem timing uilabel updating , uitextfield delegate function
- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string;˚
the replacement string doesn't added until yes returned above function. need update labels either after function called or during function. can't seem figure out how work. uilabel 1 character behind. below code in general section.
- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string { if([self.hiddentextfieldfortimeminutes.text length] == 2 && [self.hiddentextfieldfortime.text length] == 2 && ![string isequaltostring:@""]) { return no; } [self synctextfieldsminutesandhours: string]; // returns default yes; return yes; } - (void) setaccessorylabel: (nsstring *) hourstring minutesstring: (nsstring *) minutestring { timeaccessorylabel.text = [nsstring stringwithformat:@"%@:%@", hourstring, minutestring]; } - (void) synctextfieldsminutesandhours: (nsstring *) string { // these textfields nsmutablestring *hoursstring = [nsmutablestring stringwithformat: @"%@", self.hiddentextfieldfortime.text]; nsmutablestring *minutesstring = [nsmutablestring stringwithformat: @"%@", self.hiddentextfieldfortimeminutes.text]; if([self.hiddentextfieldfortimeminutes.text length] == 2 && ![string isequaltostring: @""]) { [hoursstring appendstring: [nsstring stringwithformat:@"%c", [minutesstring characteratindex:0]]]; [self.hiddentextfieldfortime settext:[nsstring stringwithformat:@"%@", hoursstring]]; [self.hiddentextfieldfortimeminutes settext: [self.hiddentextfieldfortimeminutes.text substringfromindex:1]]; } else if([self.hiddentextfieldfortimeminutes.text length] == 2 && [string isequaltostring: @""]) { // hours has nothing in if([hoursstring length] == 0) { return; } else if([hoursstring length] == 1) { // since timing of add , remove of string done return of delegate append string beginning first return. [self.hiddentextfieldfortimeminutes settext: [nsstring stringwithformat:@"%c%@", [self.hiddentextfieldfortime.text characteratindex:0], self.hiddentextfieldfortimeminutes.text]]; [self.hiddentextfieldfortime settext:@""]; } else if ([hoursstring length] == 2) { // since timing of add , remove of string done return of delegate append string beginning first return. [self.hiddentextfieldfortimeminutes settext: [nsstring stringwithformat:@"%c%@", [self.hiddentextfieldfortime.text characteratindex:1], self.hiddentextfieldfortimeminutes.text]]; [self.hiddentextfieldfortime settext: [nsstring stringwithformat:@"%c", [self.hiddentextfieldfortime.text characteratindex:0]]]; } } [self setaccessorylabel: self.hiddentextfieldfortime.text minutesstring:self.hiddentextfieldfortimeminutes.text]; }
yes. text of textfield in textfield:shouldchangecharactersinrange:replacementstring:
still have old value, because gets changed after answered yes question if text should change.
you have 2 options.
create nsstring
textfield have after returned yes yourself:
- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string { if([self.hiddentextfieldfortimeminutes.text length] == 2 && [self.hiddentextfieldfortime.text length] == 2 && ![string isequaltostring:@""]) { return no; } nsstring *realstring = [textfield.text stringbyreplacingcharactersinrange:range withstring:string]; [self synctextfieldsminutesandhours: realstring]; // returns default yes; return yes; }
or add ibaction gets called after editing took place:
- (void)viewdidload { // viewdidload implementation [textfield addtarget:self action:@selector(textfielddidchange:) forcontrolevents:uicontroleventeditingchanged]; } - (ibaction)textfielddidchange:(uitextfield *)sender { [self synctextfieldsminutesandhours: sender.text]; }
Comments
Post a Comment