使用UIControlEventEditingChanged的resignFirstResponder会导致应用崩溃

时间:2014-07-15 13:18:47

标签: ios objective-c uitextfield

我在我的.h中设置了UITextFieldDelegate,在我的.m中设置了@property (strong, nonatomic) IBOutlet UITextField *myPhoneNumber;

在我的viewDidLoad方法中我有

[self.myPhoneNumber setDelegate:self];

[self.myPhoneNumber addTarget:self
                  action:@selector(editingChanged:)
        forControlEvents:UIControlEventEditingChanged];

我的editingChanged:方法侦听以确保文本输入为> 9个字符,然后启用按钮

- (IBAction)editingChanged:(id)sender {
    if ([self.myPhoneNumber.text length] <= 9) {
        self.myButton.enabled = NO;
    }
    else {
        self.myButton.enabled = YES;
    }
}

这正是我想要的方式,但是当我按下键盘上的返回来隐藏它时,这会导致应用程序崩溃并出现错误:

2014-07-15 08:08:27.089 sample-chat[47702:90b] -[MyViewController editingChanged]: unrecognized selector sent to instance 0x10ba7420
2014-07-15 08:08:27.093 sample-chat[47702:90b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyViewController editingChanged]: unrecognized selector sent to instance 0x10ba7420'

我返回键盘的方法如下

 -(BOOL) textFieldShouldReturn:(UITextField *)textField{
        [textField resignFirstResponder];
        return YES;
 }

4 个答案:

答案 0 :(得分:0)

尝试这种方式@mostly

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger length = editingTextField.text.length - range.length + string.length;
    if (length > 0) {
        yourButton.enabled = YES;
    } else { 
        yourButton.enabled = NO;
    }
    return YES;
}

答案 1 :(得分:0)

您将editingChanged:的返回类型指定为IBAction。只有在Interface Builder中链接此方法时才需要这样做,因为您在UIControlEventEditingChanged中手动添加了viewDidLoad侦听器,这将是多余的。

使用

- (void)editingChanged:(id)sender

OR

按原样使用该方法,但将其链接到Interface Builder中的UIControlEventEditingChanged

答案 2 :(得分:0)

oky你也可以尝试使用NSNotificationCenter来做这件事,例如你想检查输入到文本字段的每个字符然后你可以试试这个

例如

//add the observer
- (void)viewDidAppear:(BOOL)animated
 {
   [super viewDidAppear:animated];
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editingChanged:) name:UITextFieldTextDidChangeNotification object:nil];
}

//remove the observer
 - (void)viewDidDisappear:(BOOL)animated
{
   [super viewDidDisappear:animated];
   [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)editingChanged:(id)sender
{
   if ([self.myPhoneNumber.text length] <= 9) {
      self.myButton.enabled = NO;
   }
   else {
      self.myButton.enabled = YES;
  }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
   [textField resignFirstResponder];
   return YES;
 }

答案 3 :(得分:0)

我认为没有必要像你那样使用自定义选择器。最好使用文本字段委托方法来管理它。

设置代理人[self.myPhoneNumber setDelegate:self]; 并实现委托方法,如:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    self.myButton.enabled = NO;
    if (textField == _myPhoneNumber)
    {
        if (textField.text.length > 9)
        {
            self.myButton.enabled = YES;
        }
    }
}


-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    return YES;
}