2016-05-30 92 views
0

我有出口的UITextField的连接,并写了这码的UITextField textfieldchange事件不会触发

-(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{不会触发 无法明白我要去哪里错了

任何帮助,将不胜感激

+0

请用 - (BOOL)文本框:(*的UITextField)文本框shouldChangeCharactersInRange:(NSRange)范围replacementString:(的NSString *)字符串 – Rajesh

+0

@Honey,我贴下面的答案,但你在' - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField'返回错误值的IF语句。用'return YES'交换'return NO',反之亦然。整个方法见下面的答案。 – thephatp

+0

@霍尼,幸运吗?我的回答是否有帮助? – thephatp

回答

0

的代码替换方法

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

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

即使“shouldChangeCharactersInRange”未被调用。但是,当我注释掉“textFieldShouldBeginEditing”,然后调用它的 – Honey

0

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

在您的.h文件中采用UITextFieldDelegate

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController<UITextFieldDelegate> 

,然后在.m文件

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

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

+1

我也做了同样的事情 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField正在调用,但不是 - (void)textFieldDidChange:(UITextField *)theTextField {I0}注释掉“textFieldShouldBeginEditing”,然后调用“textFieldDidChange”。让我知道我哪里出错了 – Honey

0

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

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

您的if语句测试是否应该开始编辑的textField与txtName相同。如果确实如此,则返回而不是。我认为你只需要交换你的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; 
}