2025-06-14

原問題:

如何在iphone中隱藏鍵盤?

有兩個uiTextFields ,但是按瞭返回鍵之後鍵盤不能隱藏。

不知道該怎麼辦

 

在你的View或ViewController開始時要設置UITextFieldDelegate 協議:

[plain]  

@interface YourViewController : UIViewController <UITextFieldDelegate>  

 

然後在.m文件中添加UITextFieldDelegate Protocol方法:

[plain]  

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

 

最後,初始化.m文件中的textfield後,

[plain] 

yourTextField=[[UITextField alloc] initWithFrame:yourFrame];  

 

下一行非常重要

[plain]  

yourTextField.delegate = self; //viewcontroller或者view關聯到textField  

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *