【问题标题】:UITextfield textfieldchange event not firedUITextfield textfieldchange 事件未触发
【发布时间】:2016-05-30 12:44:53
【问题描述】:

我已经将 UITextField 的连接导出并编写了这段代码

-(void)viewDidLoad
{
   [txtName addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

}

-(void)textFieldDidChange :(UITextField *)theTextField{
NSLog(@"Text changed");
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField==txtName)
    {
          [self.view endEditing:YES];
[self performSelector:@selector(ShowNames) withObject:nil afterDelay:0.1];
return NO;
}
return YES;

}

但是-(void)textFieldDidChange :(UITextField *)theTextField{ 没有被解雇 不明白我哪里错了

任何帮助将不胜感激

【问题讨论】:

  • 请使用 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  • @Honey,我在下面发布了答案,但是您在-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 中的 IF 语句返回了错误的值。将您的return NO 与return YES 交换,反之亦然。请参阅下面的整个方法的答案。
  • @Honey,运气好吗?我在下面的回答有帮助吗?

标签: ios iphone xcode ipad uitextfield


【解决方案1】:

替换代码方法

-(void)textFieldDidChange :(UITextField *)theTextField{
NSLog(@"Text changed");
}

与

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

【讨论】:

  • 甚至“shouldChangeCharactersInRange”也没有被调用。但是当我注释掉“textFieldShouldBeginEditing”时,它被称为
【解决方案2】:

如果你想 UITextField 事件你应该使用UITextFieldDelegate

在您的 .h 文件中采用 UITextFieldDelegate

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>

然后在你的 .m 文件中

- (void)viewDidLoad
{
    [super viewDidLoad];
     [_txtName setDelegate:self];
}

如果您有任何问题,请告诉我

【讨论】:

  • 我也做了同样的事情,-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 被调用,但不是 -(void)textFieldDidChange :(UITextField *)theTextField{ 当我注释掉“textFieldShouldBeginEditing”然后调用“textFieldDidChange”。让我知道我哪里出错了
【解决方案3】:

您的想法是正确的,但您的问题在于您的 -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 方法。

现在,在编辑之前调用这个方法,如果方法返回 NO,那么-(void)textFieldDidChange :(UITextField *)theTextField 方法将不被调用。

您的 if 语句测试应该开始编辑的 textField 是否与 txtName 相同。如果是,则返回 NO 而不是 YES。我认为你只需要交换你的 NO 和 YES 就可以了。

试试这个:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if(textField==txtName)
    {
          [self.view endEditing:YES];
          [self performSelector:@selector(ShowNames) withObject:nil afterDelay:0.1];
          return YES;
    }
    return NO;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 2019-01-31
    • 2013-04-09
    • 2015-07-25
    • 2017-06-25
    • 2012-02-21
    相关资源
    最近更新 更多