2014-10-30 39 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; 
     }