Response chain nextResponder

See an article on cocochina nextResponder skillfully use UIResponder event transfer, found the UIResonder attribute of the class. Due to the content of the original content is more, so I will be a special knowledge of the point of finishing. This is a very useful property for UIView, nextResponder is its SuperView or ViewController, so in the event of the transfer can not be set up delegate or block.

Before I look at an example, I’ll start with 2 points:
  • Event response chain: when the user clicks on a UIView, the system generates an event and puts it into the UIApplication event queue. Then the event will be passed along the chain to the user clicks on the UIView:UIApplication-> UIWindow-> RootView->; -> Subview. And then start processing the event, if the Subview does not handle, the event will be passed to the view controller, if there is no controller is passed to the superView, the last to UIWindow, UIApplication. If UIApplication does not handle the event to nil.
Response chain nextResponder
source: Bole online -alanwangmodify.png
  • UIResponder has the nextResponder property, that is, its SuperView or UIViewConterller, etc.. UIView is a subclass of UIResponder, so UIView and its subclasses can use this property.

Well, now you can look at this application example: click on the cell button, the event will be passed to ViewController, showing the click of the cell index. Before using the nextResponder attribute, the event from the cell button to the ViewController need to be VIewController as a parameter to the cell or set the proxy and block.

Response chain nextResponder
Paste_Image.png
Response chain nextResponder
Paste_Image.png

1 first create a taxonomy for UIResponder,

#import< UIKit/UIKit.h> @interface UIResponder (Router) - (void) routerEvent: (ID) info; @end #import "UIResponder+Router.h" @implementation UIResponder (Router) - (void) routerEvent: (ID) info{[self.nextResponder routerEvent:info] @end;}

2 then add a UITableview and UILable in ViewController;

#import "ViewController.h" #import "TableViewCell.h" #import "UIResponder+Router.h" @interface (ViewController) < UITableViewDataSource> @property (strong, nonatomic) UILabel *mLbl @property (strong, nonatomic); UITableView *mTableView; @end @implementation ViewController (void) viewDidLoad {[super viewDidLoad]; Do any additional setup after loading / the view, typically from a nib. CGFloat w0 CGFloat = self.view.bounds.size.width; H0 = 80; _mLbl = [[UILabel alloc]initWithFrame:CGRectMake (0, 0, W0, H0); _mLbl.backgroundColor [UIColor = orangeColor]; _mLbl.textColor = [UIColor whiteColor]; _mLbl.font [UIFont = systemFontOfSize:35.]; _mLbl.textAlignment = NSTextAlignmentCenter; CGFloat = w0 w1; CGFloat H1 = self.view.bounds.size.height - H0; CGFloat Y1 = H0; _mTableView = [[UITableView alloc]initWithFrame:CGRectMake (0, Y1, W1, H1); _mTableView.dataSource = self; [_mTableView registerClass:[TableViewCell class] forCellReuseIdentifier:@ "CELL"]; [self.view addSubview:_mLbl]; [self.view addSubview:_mTableView];} #pragma mark UITableViewDataSource (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section{return 20;} - (UITableViewCell *) tableView: (* UITableView) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath{TableViewCell *cell [tableView dequeueReusableCellWithIdentifier:@ = "CELL" forIndexPath:indexPath]; [cell reloadData: indexPath.row]; return Cell;} / / receive event - (void) _mLbl.text = routerEvent: (ID) info{info @end;}

TableViewCell;

#import < UIKit/UIKit.h> @interface; TableViewCell: UITableViewCell - (void) reloadData: (NSInteger) index; @end #import "TableViewCell.h" #import "UIResponder+Router.h" @interface (TableViewCell) @property (assign, nonatomic) NSInteger mIndex @property (strong, nonatomic); UIButton *mBtn; @end @implementation TableViewCell (void) reloadData: (NSInteger) index{= _mIndex index; [_mBtn setTitle:[NSString stringWithFormat:@ "%li" _mIndex] forState:UIControlStateNormal];} - (instancetype) initWithStyle: (UITableViewCellStyle) style reuseIdentifier: (NSString *) reuseIdentifier{self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) {_mBtn = [[UIButton alloc] initWithFrame:CGRectMake (0, 0, self.f Rame.size.width*0.5, self.frame.size.height*0.8); _mBtn.backgroundColor = [UIColor purpleColor]; [_mBtn addTarget:self action:@selector (btnAction) forControlEvents:UIControlEventTouchUpInside] [self addSubview:_mBtn]; return self;};} / / transfer event (void) btnAction{[self routerEvent:[NSString stringWithFormat:@%li, _mIndex]] @end;}

Generally speaking, get nextResponder to get the next responder view, as to how to operate, you can expand.