0

我正在使用 UITextField 输入值,将其委托设置为父视图控制器,在使用 textField.text = @"some text" 为文本字段设置文本值后,委托函数 textField:shouldChangeCharactersInRange:replacementString: 不能再调用了。如何防止这种情况或允许在 setText 之后调用此函数?提前谢谢,对不起我的英语不好。更新: textField:shouldChangeCharactersInRange:replacementString: 当我第二次点击“退格”时无法调用。

4

2 回答 2

0

如果您的意思是委托中的方法完全停止调用,则设置文本字段不是原因。你需要做其他事情才能导致这种情况。正如@rdelmar 所说,它不会被调用来进行分配,但会被调用以进行未来的编辑。例如,如果我写...

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.textField.text = @"View will appear";
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"Replacement: %@", string);
    return YES;
}

...我懂了...

2012-11-25 21:02:34.456 Hello World[6669:c07] Replacement: H
2012-11-25 21:02:34.954 Hello World[6669:c07] Replacement: e
2012-11-25 21:02:35.838 Hello World[6669:c07] Replacement: l
2012-11-25 21:02:35.974 Hello World[6669:c07] Replacement: l
2012-11-25 21:02:36.162 Hello World[6669:c07] Replacement: o
于 2012-11-26T02:07:07.280 回答
0

That method isn't supposed to be called when you set the text in code. Why would you want it to -- you are passing in the exact text you want, so there should be no need to change or validate it.

于 2012-11-26T01:59:10.817 回答