FirstResponder 释放问题

View的FirstResponder的释放问题

  今天遇到一个问题,当我隐藏掉一个正在接受用户输入的UITextField的时候,键盘并不会消失,而且键盘仍然接受用户输入,再次显示该TextField时候发现在隐藏状态下,所有的输入仍然传输到了该TextField中,于是查下官方资料找到如下解释:

Important If you hide a view that is currently the first responder, the view does not automatically resign its first responder status. Events targeted at the first responder are still delivered to the hidden view. To prevent this from happening, you should force your view to resign the first responder status when you hide it.    

  意思是如果这个View是当前的第一响应者的时候,隐藏该View并不会自动放弃其第一响应者的身份,而且会继续以第一响应者的身份接受消息。我们可以通过在隐藏View之前,手动调用resignFirstResponder来强制该view放弃第一响应者身份。

  下面请看小例子:

FirstResponder 释放问题FirstResponder 释放问题SvTestFirstResponder.h
//
// SvTestFirstResponder.h
//
// Created by maple on 3/15/12.
// Copyright (c) 2012 SmileEvday. All rights reserved.
//
// 当一个view时当前响应者时,调用其hidden方法并不会自动放弃第一响应者身份,所有的消息仍然会发送到这个view
// 可以通过在hidden前强制放弃第一响应者,恢复正常的消息传递
//

#import <UIKit/UIKit.h>

@interface SvTestFirstResponder : UIView {
UITextField *_inputField;
}

@end
FirstResponder 释放问题FirstResponder 释放问题 SvTestFirstResponder.m
//
// SvTestFirstResponder.m
//
// Created by maple on 3/15/12.
// Copyright (c) 2012 SmileEvday. All rights reserved.
//

#import "SvTestFirstResponder.h"

@interface SvTestFirstResponder()

- (void)hiddenInputView:(id)sender;
- (void)showInputView:(id)sender;

@end

@implementation SvTestFirstResponder

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code

_inputField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
_inputField.center = CGPointMake(160, 50);
[_inputField setFont:[UIFont systemFontOfSize:24]];
_inputField.text = @"input you text";
_inputField.clearsOnBeginEditing = YES;
_inputField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_inputField.borderStyle = UITextBorderStyleRoundedRect;
[self addSubview:_inputField];
_inputField.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

UIButton *hiddenBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
hiddenBtn.frame = CGRectMake(0, 0, 115, 40);
hiddenBtn.center = CGPointMake(80, 110);
[hiddenBtn setTitle:@"Hide TextField" forState:UIControlStateNormal];
[hiddenBtn addTarget:self action:@selector(hiddenInputView:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:hiddenBtn];
hiddenBtn.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
hiddenBtn.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;

UIButton *showBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showBtn.frame = CGRectMake(0, 0, 115, 40);
showBtn.center = CGPointMake(240, 110);
[showBtn setTitle:@"Show TextField" forState:UIControlStateNormal];
[showBtn addTarget:self action:@selector(showInputView:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:showBtn];
showBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
showBtn.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
}
return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/

- (void)hiddenInputView:(id)sender
{
_inputField.hidden = YES;
}

- (void)showInputView:(id)sender
{
_inputField.hidden = NO;
}

@end

  这个简单的例子中,当输入框进入接受用户输入状态的时候,点击hide按钮,键盘并不会消失而且会继续接收用户输入并且将用户输入传到TextField中去,后面再点击Show按钮的时候你会发现所有在隐藏状态下输入的文字都已经成功的被接收。我们可以修改hide方法如下:

- (void)hiddenInputView:(id)sender
{
    if (_inputField.isFirstResponder) {
        [_inputField resignFirstResponder];
    }
    _inputField.hidden = YES;
}

  这样就可以在隐藏之前强制释放第一响应者身份,这个问题比较细节,但有时候可能就是这种细节问题导致一些莫名奇妙的问题,在隐藏一些可能成为第一响应者的view之前添加强制释放第一响应者身份,可能会帮我们避免一些奇怪的问题,而且也几乎不会有什么开销,何乐而不为呢。





部门招人: 高级iOS、Android、前端开发,有意私聊,博主请你喝️
如果觉得本文帮到了你,记得点赞哦,当然也可以请博主喝一杯豆浆
FirstResponder 释放问题 FirstResponder 释放问题
微信二维码 QQ二维码
上一篇:Excel 2016中的新增函数之CONCAT


下一篇:如何在VS2008中的Language Integrated Query动态条件查询