初识 resignFirstResponder

- (void)addTask:(id)sender
{
    // Get the to-do item
    NSString *t = [taskField text];
    // Quit here if taskField is empty
    if ([t isEqualToString:@""]) {
return; }
    // Add it to our working array
    [tasks addObject:t];
    // Refresh the table so that the new item shows up
    [taskTable reloadData];
    // And clear out the text field

    [taskField setText:@""];


    // Dismiss the keyboard
    [taskField resignFirstResponder];

}

What’s this resignFirstResponder business? Here’s the short version:
Some view objects are also controls – views that the user can interact with. Buttons, sliders, and text fields are examples of controls. When there are controls on the screen, one of them can be the first responder. Having first responder status means that the control gets dibs on handling any text input from the keyboard or any shake events (such as “Shake to Undo”).
When the user taps a control that can accept first responder status, that control is sent the becomeFirstResponder message. Until another control becomes the first responder or the current control is sent the resignFirstResponder message, that control will keep this status and receive keyboard and shake input.

翻译过来是这样的:有些视图对象也是控件--视图,也就是说,用户可以和它们交互,按钮,滑块,以及文本输入框都是这种控件的的例子,当这些控件在屏幕上时,它们中的一员可以作为第一响应者(first responder).拥有第一响应者的状态也就意味着这些控件可以处理所有从键盘上输入的文本或者是摇动事件(比如"摇动停止");


当用户点击一个可以拥有第一响应者状态的控件的时候,这个控件就会被发送becomeFirstResponder 消息.直到另外一个控件变成第一响应者或者是当前的控件被发送resignFirstResponder消息(例如[taskField resignFirstResponder]),在这之前,这个控件将维持这种第一响应者的状态并接受键盘输入和摇动输入(也就是如果没有[taskField resignFirstResponder]).

以上代码效果如下:文本框输入play the dogs并点击insert之后,下面的tableview会多出一行play the dog,同时,键盘消失(即[taskField resignFirstResponder])

                        


输入然后将程序增加了一个文本框:

- (void)addTask:(id)sender{
    //Get the to-do item
    NSString *t1 = [taskField text];
    NSString *t2 = [taskField2 text];
    //Quti here if taskField is empty
    if ([t1 isEqualToString:@""]&&[t2 isEqualToString:@""]) {
        return;
    }
    
    //Add it to our working array
    if (![t1 isEqualToString:@""]&&![t2 isEqualToString:@""]) {
        [tasks addObject:t1];
        [tasks addObject:t2];
    }else if ([t1 isEqualToString:@""]){
        [tasks addObject:t2];
    }else{
        [tasks addObject:t1];
    }
    
    //Refresh the table so that the new item shows up
    [taskTable reloadData];//refresh
    //And clear out the text field
    [taskField setText:@""];
    [taskField2 setText:@""];

    //Dissmiss the keyboard
    [taskField resignFirstResponder];


:


此时如果点击下面的文本框,则下面的文本框成为第一响应者,输入paly the dog后并点击insert,键盘不消失,tableView多出行显示play the dog,然后点击上面的文本框,输入sale the dog,此时上面的文本框成为第一响应者,然后点击insert后键盘消失,因为 [taskField resignFirstResponder],也就是说:可以变成第一响应者状态的控件,当与其交互的时候(比如点击),那么它变成第一响应者状态,可以响应键盘等的输入,但是因为没有放弃第一响应者状态,所以键盘一直存在,只有最后的第一响应者放弃第一响应者键盘才消失.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值