我有一个文本域,&使用shouldChangeCharactersInRange
方法获取价值。
这是我的代码:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"Pressing : %@\n", string);
NSMutableString *text = [NSMutableString stringWithString:textField.text];
NSLog(@"length: %lu", (unsigned long)range.length);
NSLog(@"location: %lu\n", (unsigned long)range.location);
NSLog(@"before text: %@", textField.text);
[text replaceCharactersInRange:range withString:string];
NSLog(@"After replacement , Text: %@\n\n", text);
// My work..
return YES;
}
我首先将字符'T'添加到textfield,它工作正常..
2016-03-02 10:06:23.162 MyTextFieldApp[12006:4323645] Pressing : T
2016-03-02 10:06:23.163 MyTextFieldApp[12006:4323645] length: 0
2016-03-02 10:06:23.163 MyTextFieldApp[12006:4323645] location: 0
2016-03-02 10:06:23.163 MyTextFieldApp[12006:4323645] before text:
2016-03-02 10:06:23.163 MyTextFieldApp[12006:4323645] After replacement , Text: T
然后,我退格它以移除角色,这也很好..
2016-03-02 10:06:30.979 MyTextFieldApp[12006:4323645] Pressing :
2016-03-02 10:06:30.980 MyTextFieldApp[12006:4323645] length: 1
2016-03-02 10:06:30.980 MyTextFieldApp[12006:4323645] location: 0
2016-03-02 10:06:30.980 MyTextFieldApp[12006:4323645] before text: T
2016-03-02 10:06:30.981 MyTextFieldApp[12006:4323645] After replacement , Text:
但是,当我要添加新角色时,它会在replaceString上崩溃..
注意:这次,Range Gives长度为0&位置1,第一次也是当我将T添加到空文本字段时,它给出长度0以及位置0。
2016-03-02 10:07:02.158 MyTextFieldApp[12006:4323645] Pressing : T
2016-03-02 10:07:02.158 MyTextFieldApp[12006:4323645] length: 0
2016-03-02 10:07:02.159 MyTextFieldApp[12006:4323645] location: 1
2016-03-02 10:07:02.159 MyTextFieldApp[12006:4323645] before text:
2016-03-02 10:07:02.159 MyTextFieldApp[12006:4323645] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString replaceCharactersInRange:withString:]: Range or index out of bounds'
我无法发现问题出在哪里?
答案 0 :(得分:4)
您的程序崩溃是因为您试图在text
可变字符串中的无效位置插入数据。
第二次按'T'时,text
(从textField.text
检索)的值为空字符串(字面意思为@""
)。然后,您拨打replaceCharactersInRange
,传递location
1和length
0。当您使用replaceCharactersInRange
0来呼叫length
时,您实际上是在说“插入字符串”在location
位置。此函数仅在length
大于0时替换文本。
当您的代码尝试在索引1处插入字母“T”时,它不能(空字符串的第一个也是唯一有效的索引为0),因此程序崩溃。
此外,对于它的价值,如果textField为空,则程序不应返回1的位置和0的长度。必须有其他事情发生。我在Swift中编写了示例程序并获得了以下输出:
import UIKit
class ViewController: UIViewController {
var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blackColor()
textField = UITextField(frame: CGRectMake(20, 100, 200, 20))
textField.backgroundColor = UIColor.whiteColor()
textField.delegate = self
view.addSubview(textField)
}
}
extension ViewController: UITextFieldDelegate {
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
print("range.length: \(range.length), range.location: \(range.location), text: '\(textField.text!)' string: '\(string)'")
return true
}
}
输出:
range.length: 0, range.location: 0, text: '' string: 'T' // press T
range.length: 1, range.location: 0, text: 'T' string: '' // press backspace
range.length: 0, range.location: 0, text: '' string: 'T' // press T
正如您所看到的,我在T的第二个插入上看到预期的0,0范围。目标C程序的行为应该类似。