您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 學習筆記:UITextField 的常用用法整理

編輯:iOS 開發綜合

UITextField 是輸入交互裡最基礎的控件,實際開發中圍繞它的操作主要有三類:代理回調、實時取值、鍵盤通知。下面逐一整理。

一、UITextFieldDelegate 常用代理方法

開始輸入的監聽(返回 NO 可以禁止編輯):

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;

開始編輯後的回調:

- (void)textFieldDidBeginEditing:(UITextField *)textField;

結束輸入的監聽:

- (void)textFieldDidEndEditing:(UITextField *)textField;

是否允許結束編輯:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;

回車鍵被點擊時(常用於收起鍵盤或跳轉到下一個輸入框):

- (BOOL)textFieldShouldReturn:(UITextField *)textField;

記得設置 delegate,並在合適的時機置為 nil(通常是 viewDidDisappear 或 dealloc 中),避免野指針問題。

二、實時獲取輸入內容

代理方法只能在特定時機回調,如果想「每敲一個字符就響應」,更合適的是添加事件監聽:

[self.accountText addTarget:self
                     action:@selector(input:)
           forControlEvents:UIControlEventEditingChanged];

- (void)input:(UITextField *)textField
{
    // 在這裡處理 textField.text 的變化
}

這個方式常用於實時校驗、實時搜索、密碼強度提示等場景。

三、監聽鍵盤事件

鍵盤彈出與收起會遮擋界面,需要通過通知來響應。共四個關鍵通知:will show、did show、will hide、did hide。

// 鍵盤即將出現
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

// 鍵盤已經出現
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

// 鍵盤即將消失
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

// 鍵盤已經消失
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardHide:)
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

- (void)keyboardWillShow:(NSNotification *)notif { }
- (void)keyboardShow:(NSNotification *)notif     { }
- (void)keyboardWillHide:(NSNotification *)notif { }
- (void)keyboardHide:(NSNotification *)notif     { }

通常只需要處理 willShow 與 willHide 兩個時機,因為此時還能拿到鍵盤的最終 frame,便於做界面偏移的動畫同步。

- (void)keyboardWillShow:(NSNotification *)notif
{
    // 鍵盤最終的 frame,位於屏幕坐標系
    CGRect keyboardFrame = [notif.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 動畫時長,用來讓自定義動畫與系統保持一致
    NSTimeInterval duration = [notif.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    // 動畫曲線
    UIViewAnimationCurve curve = [notif.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
}

最後提醒兩點:一是在 dealloc 或 viewDidDisappear 中記得 removeObserver,否則通知仍然會送達已釋放的對象;二是鍵盤 frame 是屏幕坐標系下的值,如果目標視圖不在屏幕上,需要做坐標轉換。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved