1. 程式人生 > >iOS中TextField 和 TextView 控制元件的基本使用

iOS中TextField 和 TextView 控制元件的基本使用

介紹-introduction

This time I want to create a new simple project which is a self-introduction program to show some fundamental ways to use the TextFiled controller and TextView controller.There is the UI design for the project:
本次準備寫一個簡單的可輸入的自我介紹程式來演示TextField 和 TextView 控制元件的基本使用方法,專案的介面設計如下:
專案介面設計


Depending on our user interface we need to add some controllers and make some contact relationship between code and controllers.So there is a list of the names of controllers.Please contact those controller by yourself.
根據介面我們需要新增和相關控制元件和設定控制元件關聯的關係,具體需要的控制元件名見下圖,控制元件關聯程式碼可自行關聯。
這裡寫圖片描述

開發程式碼詳解-Developing Code Explanation

First of all , we need to contact some operation’s protocol to the ViewController we want to use to show TextField and TextView.We can see some code in ViewController.h:
首先,我們需要讓我們所使用的這個ViewController 關聯相關的操作協議。在ViewController.h中:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UITextFieldDelegate,UITextViewDelegate>
//UIviewController實現了兩個委託(即協議)UITextFieldDelegate,UITextViewDelegate。他們中存在一些能夠控制UITextFiled 和 UITextView的方法。 //UIviewController need to achieve two protocols which are UITextFiledDelegate and UITextViewDelegate.There are some methods in these protocols to control the lifcycle of UITextFiled and UITextView. @end

After we have finished ,we can use some method in these protocol to achieve some operation we need.For example , we can achieve some fundamental function just like input some content.So we need to call some methods in these protocol to achieve our demands.Because when we click TextField and TextView the operating system will automatically call keyboard,but there are no any notification for us to see something.So we can set some notification to clear the process inside.
When the keyboard disappear,it is not easy just like when the appearance of the keyboard.We need to do one thing is that we need to cancel keyboard’s “First Responder Position”.As we can see in ViewController.m:
在關聯了相關的操作協議之後,我們就可以對這些控制元件進行我們需要的操作,比如我們需要輸入一些我們需要的內容這樣最基本的功能,因此我們就需要呼叫一些協議中的方法來實現我們所需要的功能。因為在點選TextField 或 TextView控制元件之後,系統會自動呼叫鍵盤的出現。但是不會採取任何通知,我們可以設定一些通知來看看其中內部的過程;在鍵盤消失的時候,就不像鍵盤出現的時候那麼容易,我們需要做一件事就是取消鍵盤所處在的“第一想響應者的位置”。具體我們可以在ViewController.m中呼叫以下幾個方法來實現:
ViewController.m:
鍵盤通知註冊(Resign the keyboard’s notification):

-(void)viewWillAppear:(BOOL)animated{
    //註冊鍵盤出現通知
    //Resign the notification of the appearance of keyboard.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    //註冊鍵盤隱藏通知
    //Resign the notification when the keyboard is hidden.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    [super viewWillAppear:animated];
}

解除鍵盤通知(Relieve the keyboard’s notification:):

#pragma mark - viewWillDisappear
-(void)viewWillDisappear:(BOOL)animated{
    //解除鍵盤出現通知
    //relieve the notification of appearance of keyboard.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    //解除鍵盤隱藏通知
    //relieve the notification when the keyboard is hidden.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    [super viewWillDisappear:animated];
}

取消鍵盤的“第一響應者的位置”(Concel the keyboard’s “First Responder Position”):

#pragma mark - UITextFiled Delegate Method
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];//通過委託來放棄“第一響應者的位置”
    //Though protocol we can make the keyboard give up the "First Responder Position".
    /**********************************************************************************************
    Discussion
    The default implementation returns YES, resigning first responder status. Subclasses can override this method to update state or perform some action such as unhighlighting the selection, or to return NO, refusing to relinquish first responder status. If you override this method, you must call super (the superclass implementation) at some point in your code.
     **********************************************************************************************/
    return YES;
}//此方法是UITextFieldDelegate委託協議中的方法,在使用者點選鍵盤時候呼叫,其中的[textField resignFirstResponder]用來關閉鍵盤。
//This method is one of UITextFieldDelegate protocol.When users click the textfiled and the keyboard appears,[textField resignFirstResponder] is used to close the keyboard.
#pragma mark - UITextView Delegate Method
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        //通過委託來放棄“第一響應者的位置”
        //Though protocol we can make the keyboard give up the "First Responder Position".
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}//此方法是由UITextViewDelegate委託協議中的方法,在使用者點選鍵盤時被呼叫。
//This method is one of UITextViewDelegate protocol.if the uesrs click the textview,the method will be quited.

實現鍵盤通知(Achieve the keyboard notification:):

#pragma mark - keyboardControl_Notification
-(void)keyboardDidShow:(NSNotification *)notif{
    NSLog(@"鍵盤開啟");
    //發出鍵盤開啟通知
    //sent the keyboard_open_notification in console.
}
-(void)keyboardDidHide:(NSNotification *)notif{
    NSLog(@"鍵盤關閉");
    //發出鍵盤關閉通知
    //sent the keyboard_close_notification in console.
}

執行結果-runing result:

Runing result