好吧,這是之前寫的一些東西,在印象筆記中做了記錄,現(xiàn)在轉(zhuǎn)到簡書中。實現(xiàn)了在 view 中獲取控制器并進行界面跳轉(zhuǎn),寫之前沒怎么想,寫完后才發(fā)現(xiàn)這么跳轉(zhuǎn)并不好(ps:控制器的事,view 去做,有點越俎代庖),所以,只當是進行響應(yīng)者鏈條的一個demo。
下面代碼實現(xiàn)的效果是,讓子 view 接收這些事件后,同時把這些事件繼續(xù)向上傳,會一直傳到 UIApplication 為止。而在傳的過程中,如果子 view 接收了這些事件,那么事件會自然終止,我們現(xiàn)在可以做的是同時讓子 view 接收事件,而且還讓事件不終止,并繼續(xù)向上傳。
#import "myView.h"
#import "SecondViewController.h"
@interface myView()
{
UIViewController *superVC;
}
@end
@implementation myView
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"被點擊了");
superVC = [self viewController:self];
// [superVC presentViewController:[SecondViewController new] animated:YES completion:nil];
[superVC.navigationController pushViewController:[SecondViewController new] animated:YES];
}
//獲取響應(yīng)鏈中的下一級響應(yīng),獲取控制器,進行界面跳轉(zhuǎn)
- (UIViewController *)viewController:(UIView *)view{
UIResponder *responder = view;
while ((responder = [responder nextResponder]))
if ([responder isKindOfClass: [UIViewController class]])
return (UIViewController *)responder;
return nil;
}