今天我完成了第一个应用程序,我正在测试我的应用程序。好吧,我发现了一个我无法找到的错误。在我的应用程序中,我有两个UITextfields
。当用户填写其中一个时,另一个使用委托方法textField:shouldChangeCharactersInRange:replacementString:
自动填充
这是方法。当用户在任何文本字段中键入任何内容并按下iPad键盘上的撤消按钮或摇动iPhone然后按下撤消时,我的应用程序崩溃。这是我收到的错误。
(Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSBigMutableString substringWithRange:]: Range {0, 2} out of bounds; string length 0').
我非常感谢你的帮助。
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *stringToValidate = [[textField text] stringByReplacingCharactersInRange:range withString:string];
if ([stringToValidate isEqualToString:@""]) {
self.scoreToAddOfTeam1.text = nil;
self.scoreToAddOfTeam2.text = nil;
return YES;
}
if ([self validateInputsInsideAnNSString:stringToValidate]) {
if (textField == self.scoreToAddOfTeam1) {
self.scoreToAddOfTeam2.text = [self autocompleteOthersTeamPointsToAdd:stringToValidate];
} else {
self.scoreToAddOfTeam1.text = [self autocompleteOthersTeamPointsToAdd:stringToValidate];
}
} else {
if (textField == self.scoreToAddOfTeam1) {
self.scoreToAddOfTeam2.text = nil;
} else {
self.scoreToAddOfTeam1.text = nil;
}
}
return YES;
}
-(NSString *) autocompleteOthersTeamPointsToAdd: (NSString *) stringBasedToCalculate {
NSString *myString = [NSString stringWithFormat:@"%ld", (long)100 - [stringBasedToCalculate integerValue]];
return myString;
}
答案 0 :(得分:0)
我认为您必须在应用程序中更改UIUndoManager的实现。查看本教程here
如果您不想在应用中处理动态撤消,则可以使用以下方法将其停用:
[UIApplication sharedApplication].applicationSupportsShakeToEdit = NO;