IOS 隱藏鍵盤。

來源:互聯網
上載者:User

標籤:ext   os   set   代碼   io   table   

在View的UITextField中經常需要輸入完文字後隱藏軟鍵盤,要實現著一點要讓View的Controller實現UITextFieldDelegate代理,然後編寫相應的代碼。

 

  1. #import <UIKit/UIKit.h>
  2. @interface TestVeiwController : UIViewController<UITextFieldDelegate> {
  3. IBOutlet UITextField *txt;
  4. }
  5. @property (nonatomic,retain) UITextField *txt;
  6. @end

然後記得要指定文字框的代理

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. txt.delegate = self;
  4. }

點擊Enter的時候隱藏軟鍵盤:

 

  1. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  2. {
  3. [textField resignFirstResponder];
  4. return YES;
  5. }



點擊取消(Cancel)或那個小差號的時候隱藏。注意這裡如return YES則無法隱藏,我採用了點變通的方法。

  1. - (BOOL)textFieldShouldClear:(UITextField *)textField
  2. {
  3. [textField resignFirstResponder];
  4. textField.text = @”";
  5. return NO;
  6. }

點擊View的其他地區隱藏軟鍵盤。

  1. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  2. {
  3. [txt resignFirstResponder];
  4. }

這裡直接用了我自訂的變數。

設定代理的步驟比較重要,別忘記了,要不沒反應

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.