在view中獲取主控制器,進行界面跳轉(zhuǎn)

好吧,這是之前寫的一些東西,在印象筆記中做了記錄,現(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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容