使用UITextField时Settext方法无法接收UITextFieldTextDidChangeNotification

时间:2016-03-26 02:31:55

标签: ios objective-c uitextfield nsnotificationcenter

我想要自定义UITextFiled输入视图,在自定义视图中监听textField值更改。

当我使用self.textField.text = @"demo";

UITextFieldTextDidChangeNotification没有被调用,但是  UITextFieldTextDidBeginEditingNotification被召唤,

我如何获得UITextFieldTextDidChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextFieldTextDidChangeNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textBeginEdit:) name:UITextFieldTextDidBeginEditingNotification object:nil];

1 个答案:

答案 0 :(得分:0)

将ViewController指定为UITextField的委托。确保您的控制器符合UITextFieldDelegate协议,然后在控制器中实现这些委托方法。

@interface YourViewController ()<UITextFieldDelegate>
@end

-(void)viewDidLoad
{
     [super viewDidLoad]
      self.textField.delegate = self;
}

//实现这些方法

  • textFieldShouldBeginEditing:
  • textFieldDidBeginEditing:
  • textFieldShouldEndEditing:
  • textFieldDidEndEditing: