解析iOS开发中的FirstResponder第一响应对象
1. UIResonder
对于C#里所有的控件(例如TextBox),都继承于Control类。而Control类的继承关系如
下:
代码如下:
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
对于iOS里的UI类,也有类似的继承关系。
例如对于UITextField,继承于UIControl;UIControl继承于UIView,UIView继承于UIRe
sponder,UIResponder继承于NSObject。
具体架构可以参见:
http://developer.apple.com/library/ios/#documentation/general/conceptual/Devp
edia-CocoaApp/Responder.html
UIResponder是UIKit框架中的类(Mac OS X Cocoa对应的是AppKit框架)。
2. 第一响应对象
在应用的响应对象里,会有一个成为第一响应对象。
第一响应对象和其他响应对象之间有什么区别?对于普通的触摸事件没什么区别。就算
我把一个按钮设置成第一响应对象,当我点击其他按钮时,还是会响应其他按钮,而不
会优先响应第一响应对象。
第一响应对象的区别在于负责处理那些和屏幕位置无关的事件,例如摇动。
苹果官方文档的说法是:第一响应对象是窗口中,应用程序认为最适合处理事件的对象
。
一个班只能有一个班长,应用的响应对象中,只能有一个响应对象成为第一响应对象。
3. 成为与取消第一响应对象。
要当第一响应对象,还需要有View来毛遂自荐:
- (BOOL) canBecomeFirstResponder
{
returnYES;
}
如果缺少了这段,就算用[view becomeFirstResponder]也不能让一个view成为第一响应
对象。。。强扭的瓜不甜?好吧不是这个原因。大多数视图默认只关心与自己有关联的
事件,并且(几乎)总是有机会来处理这些事件。以UIButton为例,当用户单击某个UIB
utton对象时,无论当前的第一响应对象是哪个视图,该对象都会收到指定的动作消息。
当上第一响应对象吃力不讨好么。。。所以只能由某个UIResponder明确表示自己愿意成
为第一响应对象才行。(我不知道设计上是基于什么考虑。。。安全?)
在当上第一响应对象时,不同对象可能会有一些特殊的表现。例如UITextField当上的时
候,就会调出一块小键盘。
第一响应对象也有可能被辞退。发送一个resignFirstResponder,就可以劝退。
4. 第一响应对象的任务
刚才说了第一响应对象可以处理摇动。就来看个范例吧:
代码如下:
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(motion == UIEventSubtypeMotionShake)
{
NSLog(@"Device is beginning to shake");
[selfsetCircleColor:[UIColorredColor]];
[selfsetNeedsDisplay];
}
}
当摇动开始时触发某些行为。
5. 获取当前第一响应对象
源自这篇讨论:http://stackoverflow.com/questions/1823317/get-the-current-firs
t-responder-without-using-a-private-api
提问的家伙用了如下的方式来获取
UIView *firstResponder = [keyWindow
performSelector:@selector(firstResponder)];
结果被苹果打回来,说用了非公开的API。。。
于是这家伙只好苦逼地用递归了:
implementationUIView (FindFirstResponder)
- (UIView *)findFirstResponder
{
if (self.isFirstResponder) {
return self;
}
for (UIView *subView in self.subviews) {
UIView *firstResponder = [subView findFirstResponder];
if (firstResponder != nil) {
return firstResponder;
}
}
return nil;
}
@end
6.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 //
// 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 //
// 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(, , , )];
_inputField.center = CGPointMake(, );
[_inputField setFont:[UIFont systemFontOfSize:]];
_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(, , , );
hiddenBtn.center = CGPointMake(, );
[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(, , , );
showBtn.center = CGPointMake(, );
[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开发中的FirstResponder第一响应对象
自己写的一个小例子,给UITableView添加扩展,监听cell的键盘响应事件,对应修改偏移量
.h代码如下
#import <UIKit/UIKit.h> @interface UITableView (Keyboard)
/**
* 添加tableview的cell的键盘监听事件,修改对应cell的偏移量
*/
- (void)addResponserKeyboardNotification;
@end
.m代码如下
#import "UITableView+Keyboard.h"
/**
* 保存当前的偏移y值,便于在键盘隐藏时恢复修改前的偏移量
*/
static CGFloat currY = .f; @implementation UITableView (Keyboard)
- (void)addResponserKeyboardNotification{
currY = .f;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
} - (void)keyboardWillShow:(NSNotification*)noti{
// NSLog(@"----%@\n%@",noti.userInfo,noti.object); for (UITableViewCell *infoCell in self.visibleCells) {
if ([infoCell isKindOfClass:[UITableViewCell class]]) { UIView *resView = [self findFirstResponder:infoCell]; CGRect tvToView = [resView convertRect:resView.frame toView:self];
CGRect keyboardFrame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// NSLog(@"\n--tv:%@\n--kb:%@",NSStringFromCGRect(tvToView),NSStringFromCGRect(keyboardFrame));
if (CGRectGetMaxY(tvToView) + >= keyboardFrame.origin.y) {
currY = self.contentOffset.y;
CGFloat nowY = currY;
nowY += CGRectGetMaxY(tvToView) + - keyboardFrame.origin.y;
self.contentOffset = CGPointMake(, nowY);
}
// [self showMask]; } } }
/**
* 递归找到当前键盘的响应者
*/
- (UIView *)findFirstResponder:(UIView *)superView
{
if (superView.isFirstResponder) {
return superView;
}
for (UIView *subView in superView.subviews) {
UIView *firstResponder = [self findFirstResponder:subView];
if (firstResponder != nil) {
return firstResponder;
}
}
return nil;
}
/**
* 恢复键盘出现前的偏移y值
*/
- (void)keyboardWillHide:(NSNotification*)noti{
self.contentOffset = CGPointMake(, currY);
}
@end
您可能感兴趣的文章:
解析iOS开发中的FirstResponder第一响应对象的更多相关文章
- 【Swift】ios开发中巧用 description 打印对象时,打印对象的属性
ios开发中我们打印对象的时候,会直接输出对象地址,这样不方便我们开发.我们可以 巧用 description 打印对象时,输出对象的属性 在oc中直接重写即可.swift中需要遵守Printable ...
- iOS开发中didSelectRowAtIndexPath tap事件响应延迟
为UITableViewCell添加tapped事件,代码如下: class VideoViewController: UIViewController , UITableViewDataSource ...
- iOS开发中的Html解析方法
iOS开发中的Html解析方法 本文作者为大家介绍了在iOS开发中的Html解析方法,并同时提供了Demo代码的下载链接,Demo 解析了某个网站(具体可在代码中查看)的html网页,提取了图片以及标 ...
- iOS开发中的单元测试(三)——URLManager中的测试用例解析
本文转载至 http://www.cocoachina.com/cms/plus/view.php?aid=8088 此前,我们在<iOS开发中的单元测试(一)&(二)>中介绍 ...
- ios开发中的小技巧
在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIViewal ...
- iOS开发中遇到的一些问题及解决方案【转载】
iOS开发中遇到的一些问题及解决方案[转载] 2015-12-29 [385][scrollView不接受点击事件,是因为事件传递失败] // // MyScrollView.m // Creat ...
- IOS开发中UI编写方式——code vs. xib vs.StoryBoard
最近接触了几个刚入门的iOS学习者,他们之中存在一个普遍和困惑和疑问,就是应该如何制作UI界面.iOS应用是非常重视用户体验的,可以说绝大多数的应用成功与否与交互设计以及UI是否漂亮易用有着非常大的关 ...
- iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】
在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...
- 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新。
UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0 ...
随机推荐
- ABAP中Conversion Routine示例
在SAP的Domain定义中,Output Length下面有个Convers. routine的标识,这是SAP用来进行输入输出转换的.我们知道,屏幕上的I/O字段都是字符串形式的,而数 ...
- 穷举法破解 zebrone1.1
系统 : Windows xp 程序 : zebrone1.1 程序下载地址 :http://pan.baidu.com/s/1boqVcU7 要求 : 编写注册机 使用工具 :OD 可在看雪论坛中查 ...
- 在Seismic.NET下用最少的语句写出一个剖面显示程序
用Seismic.NET开发地震剖面显示程序可以节省大量的时间,下面的代码展开了如何用最少的代码显示一个SEGY文件. // 用一行语句把 reader, pipeline, view 和 plot ...
- Swift开发第五篇——四个知识点(Struct Mutable方法&Tuple&autoclosure&Optional Chain)
本篇分三部分: 一.Struct Mutable方法 二.多元组(Tuple) 的使用 三.autoclosure 的使用 四.Optional Chain 的使用 一.Struct Mutable方 ...
- mod_slotmem mod_manager mod_proxy_cluster mod_advertise Permission denied
restorecon /etc/httpd/modules/mod_slotmem.so
- iOS开发之网络编程--5、NSURLSessionUploadTask+NSURLSessionDataDelegate代理上传
前言:关于NSURLSession的主要内容快到尾声了,这里就讲讲文件上传.关于文件上传当然就要使用NSURLSessionUploadTask,这里直接讲解常用的会和代理NSURLSessionDa ...
- WPF-拖动面板移动窗口&设置窗口状态
在需要去掉窗口边框的情景下往往需要有拖动面板直接移动窗口和自定义按钮改变窗口状态和关闭的功能 拖动面板移动窗口 为面板控件添加MouseMove事件, 面板控件指的是用来当面板的控件,可以是grid. ...
- oracle表相关
堆表 数据以堆的形式管理,增加数据时会使用段中找到的第一个能放下数据的自由空间,我们见到的绝大部分的表都是堆表.堆表是数据库的默认表类型. 最简单的情况是 create table test (c1 ...
- 烂泥:ubuntu安装vmtools
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近由于工作需要,需要使用桌面版的linux系统,所以就选择了ubuntu.同时为了方便使用,就在VM中安装ubuntu. 但是为了文件以及操作的方便就 ...
- 4412开发板Android教程——Android平台简介
本文转自迅为开发板论坛:http://www.topeetboard.com Android和IOS Android的历史 Android公司 2005年Google收购成立22个月的Android公 ...