http://www.devdiv.com/thread-30694-1-1.html
我们先看一张图,看看event被处理的顺序 The general path of an event up the responder chain starts with a view—the first responder or the view under the mouse pointer or finger. From there, it proceeds up the view hierarchy to the window object and then to the global application object. However, the responder chain for events in iOS adds a variation to this path: If a view is managed by a view controller and if the view cannot handle an event, the view controller becomes the next responder.(如果一个视图被一个视图控制器管理着并且这个视图处理不了某个事件,则这个视图控制器就成为这个事件的下一级响应) 一般来说first responder就是主表或者指头触摸到的控件。 iphone中,responder chain为view->(view controller->)parent view(->parent view controller)->window->application。 |
1. 建立一个view-based application,名字为view1
2. 双击view1ViewController.xib
3. 在interface builder中选中first reponder
点菜单tools->identity inspector
在identity inspector中新加一个action,名字叫myAction1
4. 在interface builder中选中button 5. 点菜单tools->connection inspector, 点中touch up inside拖拽到first responder 并且选中myAction1 |
7. 在view1ViewController.h中加入
- (IBAction) myAction1:(id)event;
在view1ViewController.m中加入
- (IBAction) myAction1:(id)event{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Hello DevDiv"
message:@"I'm a DevDiver"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
8. 重新编译执行,点击button
现在大家知道responder chain作用了吧。
button的first responder是它自己,当它不能处理某一action后,它就交给responder chain中的controller来处理。
另外,大家也明白了interface builder中First Responder怎么使用了吧。