objective c - Why would subclassing NSFormatter prevent me from editing NSTextField's input? -
i'm trying format 2 nstextfields smpte time codes, have format: hh:mm:ss:ff
. however, when user switches smpte code drop-frame, delimiter between ss , ff needs switch ;
(hh:mm:ss;ff
). this, i've subclassed nsformatter, , have working except 1 stubborn problem.
the text field accepts input fine, if highlight-replace, backspace, delete, or insert new characters text field, nsbeep , can't switch focus away text field. can input new text if delete whole text field first, not if try edit existing input. here implemented methods/overrides:
- (nsstring*)stringforobjectvalue:(id)obj { if ( ! [obj iskindofclass:[nsnumber class]]) { return nil; } nsmutablestring *string = [nsmutablestring stringwithstring:@"00:00:00:00"]; int length = (int)[[obj stringvalue] length]; int insertlocation = 9; if (length == 1) { [string replacecharactersinrange:nsmakerange(10, 1) withstring:[obj stringvalue]]; } else { while (length > 1) { nsstring *temp = [[obj stringvalue] substringfromindex:length-2]; [string replacecharactersinrange:nsmakerange(insertlocation, 2) withstring:temp]; obj = [nsnumber numberwithint:[obj intvalue]/100]; length -= 2; insertlocation -= 3; } if (length == 1) { [string replacecharactersinrange:nsmakerange(insertlocation+1, 1) withstring:[obj stringvalue]]; } } return string; } - (bool)getobjectvalue:(out __autoreleasing id *)obj forstring:(nsstring *)string errordescription:(out nsstring *__autoreleasing *)error { int valueresult; nsscanner *scanner; bool returnvalue = no; scanner = [nsscanner scannerwithstring: string]; [scanner scanstring:@":" intostring:null]; [scanner scanstring:@";" intostring:null]; if ([scanner scanint:&valueresult] && ([scanner isatend])) { returnvalue = yes; if (obj) { *obj = [nsnumber numberwithint:valueresult]; } } return returnvalue; }
at least @ point, don't need validate input during editing, when editing finished. tried implementing ispartialstringvalid
, returning yes
, didn't seem either. appreciated!
ok, solved doing more testing. appears though why failing because getobjectvalue
receiving string delimiters in them , not correctly removing them. replaced method this:
- (bool)getobjectvalue:(out __autoreleasing id *)obj forstring:(nsstring *)string errordescription:(out nsstring *__autoreleasing *)error { nsstring *newstring = [string stringbyreplacingoccurrencesofstring:@":" withstring:@""]; if (obj) { *obj = [nsnumber numberwithint:[newstring intvalue]]; return yes; } return no; }
now works perfectly.
Comments
Post a Comment