FirstResponder 释放问题

转自:http://www.cnblogs.com/smileEvday/archive/2012/03/18/2405190.html

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放弃第一响应者身份。

  下面请看小例子:

//
// 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
//
// 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之前添加强制释放第一响应者身份,可能会帮我们避免一些奇怪的问题,而且也几乎不会有什么开销,何乐而不为呢。

(转)FirstResponder 释放问题的更多相关文章

  1. 解析iOS开发中的FirstResponder第一响应对象

    1. UIResonder 对于C#里所有的控件(例如TextBox),都继承于Control类.而Control类的继承关系如下: 代码如下: System.Object System.Marsha ...

  2. 【Java并发编程实战】----- AQS(二):获取锁、释放锁

    上篇博客稍微介绍了一下AQS,下面我们来关注下AQS的所获取和锁释放. AQS锁获取 AQS包含如下几个方法: acquire(int arg):以独占模式获取对象,忽略中断. acquireInte ...

  3. 工作中遇到的一个多线程下导致RCW无法释放的问题

    最近有个同事在调用一个类库中的方法时遇到了一个问题,异常信息如下: 尝试释放正在使用的RCW,活动线程或其他线程上正在使用该 RCW,释放正在使用的 RCW 的尝试会导致损坏或数据丢失. 该方法中对w ...

  4. Activity的释放

    1.ActivityTwo.finish(); 在你的activity动作完成的时候,或者Activity需要关闭的时候,调用此方法.当你调用此方法的时候,系统只是将最上面的Activity移出了栈, ...

  5. CentOS7清理yum缓存和释放内存方法

    清理yum缓存 清理yum缓存使用yum clean 命令,yum clean 的参数有headers, packages, metadata, dbcache, plugins, expire-ca ...

  6. 在dll里malloc/new/cvCreate分配内存,在exe里free/Releases释放内存时会出错。

    写了个程序,在DLL中用malloc分配了一块内存,但是在exe程序中释放,结果程序crash,原因就是:其原因可能是堆被损坏,这也说明 TestMySticker.exe 中或它所加载的任何 DLL ...

  7. Delphi的分配及释放---New/Dispose, GetMem/FreeMem及其它函数的区别与相同

    转载自:http://www.cnblogs.com/qiusl/p/4028437.html?utm_source=tuicool 我估摸着内存分配+释放是个基础的函数,有些人可能没注意此类函数或细 ...

  8. PHP unset()函数销毁变量 但没有实现释放内存

    <?PHP $a = "hello";$b = &$a;unset( $b );echo $a; // 输出 helloecho $b; // 报错$b = &quo ...

  9. Delphi 中的自动释放策略-转

    八.使用结构体而不是结构体指针: 很重要 一.指定 Owner 后, 随 Owner 连带释放: //uses Vcl.StdCtrls, Vcl.ExtCtrls; var panel: TPane ...

随机推荐

  1. rdtsc获取时间统计程序的运行效率

    __u64 rdtsc() {         __u32 lo,hi;           __asm__ __volatile__         (          "rdtsc&q ...

  2. this的使用、继承、super

    1.this的使用 1)可以用于区分局部变量 Person(int age,string name) { this.age=age; this.name=name; } 2)构造方法中,用this调用 ...

  3. nginx规则和ci的支持

    CI框架下nginx重写规则,不再404 http://blog.csdn.net/EI__Nino/article/details/8599304 server { listen 80; serve ...

  4. iOS字符串大小转换

    NSString *test = @"Hello World";            // 小写      NSString *lower = [test lowercaseSt ...

  5. 只 一行显示可左右滚动的文本(UITextField中文限制)

    // // ViewController.m // 一行显示可滚动的文本 // // Created by apple on 15-5-8. // Copyright (c) 2015年 apple. ...

  6. POJ 2127

    #include <iostream> #define MAXN 501 using namespace std; int a[MAXN],b[MAXN],ans[MAXN]; int G ...

  7. Android中的Adapter总结

    一.Adapter的介绍 An Adapter object acts as a bridge between an AdapterView and the underlying data for t ...

  8. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  9. 用Stopwatch类获得程序运行时间

    我们可以用Stopwatch类获得程序的运行时间,在优化代码时,可以用此方法来查看优化前后程序所耗费的时间 //Stopwatch类別在System.Diagnostics命名空间里 Stopwatc ...

  10. light oj 1393 - Crazy Calendar 博弈论

    思路:当移到右下角时,就不能移动了.所以与右下角的奇偶性相同的位置,都不能直接到达,先手必败! 只需考虑与右下角奇偶不同的位置,可以看成NIM博弈.最后NIM和不为0的胜,否者败!! 代码如下: #i ...