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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值