2014-10-30 33 views
0

我已经声明为一个IBOutletresignFirstResponder不响应

@property (strong, nonatomic) IBOutlet UITextField *fieldPassword; 

我合成它以后,然后,企图有返回键取消键盘名为“fieldPassword”的文本字段,我有:

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    fieldPassword.delegate = self; 
    [fieldPassword resignFirstResponder]; 
    [self.view endEditing:YES]; 

    return YES; 
} 

问题是,当我运行模拟器并在指定的文本字段中按回车键时,什么也没有发生。此前,我还在-viewDidLoad中有fieldPassword.delegate = self;,并且模拟器崩溃时出现unrecognized selector错误。

任何帮助非常感谢!

+2

这是没有指定文本字段委托,它得到一个方法内由代表调用。这是永远不会被达到的代码。尝试在viewDidLoad中指定它(向我们展示实际发生的崩溃),或者在viewDidAppear中。 – 2014-10-30 22:54:19

+0

1)不要同时调用'resignFirstResponder'和'endEditing:'。只是一个或另一个。 2)在你的'textFieldShouldReturn:'方法中,你应该在'textField'参数中调用'resignFirstResponder'。 3)为什么在其中一个文本字段委托方法中将'delegate'属性设置为'fieldPassword'?它应该已经设置好了。 – rmaddy 2014-10-30 22:57:55

+0

也永远不要相信你的模拟器在键盘内。 – 2014-10-30 23:37:12

回答

0

如果您之前没有设置委托,您将不会获得委托回调。在viewDidLoad试试这个:

self.fieldPassword.delegate = self; 

你可能之前已经错过了self

1

应该[self.fieldPassword resignFirstResponder];而不是[fieldPassword resignFirstResponder];

而且self.fieldPassword.delegate = self;应在viewDidLoadviewDidAppear

0

按照下面的事情

1.Go to xib or storyboard where you have set that your view. 

    2.Right Click the TextField.If you click that you can see the 

     Outlets->Delegate with Empty Circle 

    3.Just connect with File's Owner.It is Yellow Color. 

     Once you do this,the circle is not empty.It is filled now. 

    4.Then go to declaration or .h part 


     #import <UIKit/UIKit.h> 

     @interface ViewController : UIViewController<UITextFieldDelegate> 

5.Then in .m 

     -(BOOL)textFieldShouldReturn:(UITextField *)textField 

     { 

      [textField resignFirstResponder]; 

      return YES; 
     }