UITextField忽略inputDelegate‽

UITextField是否忽略了inputDelegate? 使用下面的代码:

- (void)viewDidLoad { [super viewDidLoad]; self.textField.inputDelegate = self; NSLog(@"textField: %@", self.textField); NSLog(@"delegate: %@", self.textField.inputDelegate); } 

我得到以下输出:

 2012-03-26 20:43:49.560 InputTest[33617:f803] textField: <UITextField: 0x6c093a0; frame = (20 20; 280 31); text = ''; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x6c094d0>> 2012-03-26 20:43:49.561 InputTest[33617:f803] delegate: (null) 

它运行得很好,没有任何警告或exception,委托属性工作得很好。 但是,设置inputDelegate不会导致更改,并且不会调用委托方法。

我面临同样的问题。 经过深入的search,我发现,虽然UITextInput协议是在iOS 3.2中,但UITextView / Field没有在iOS 5之前使用该协议。在iOS 5或更高版本中运行您的代码,它应该工作。

在编辑会话开始之后设置您的委托。

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { [self.myTextfield setInputDelegate:self]; NSLog(@"Inputdelegate is: %@", self.myTextField.inputDelegate); return YES; }