美文网首页
iOS事件响应链

iOS事件响应链

作者: 哥只是个菜鸟 | 来源:发表于2020-07-16 18:41 被阅读0次

    UIView-->ViewController-->UIWindow-->UIAplication-->AppDelagate

    UIView和UICLayer

    • UIView继承UIResponder,接收点击事件,CALayer直接继承 NSObject,并没有相应的处理事件的接口。
    • UIView是CALayer的delegate
    • UIView主要处理事件,CALayer负责绘制就更好

    通过视图(view)获取该视图所在的控制器,通过响应链原理查找

    + (nullable UIViewController *)findBelongViewControllerForView:(UIView *)view {
        UIResponder *responder = view;
        while ((responder = [responder nextResponder]))
            if ([responder isKindOfClass: [UIViewController class]]) {
                return (UIViewController *)responder;
            }
        return nil;
    }
    

    获取当前控制器

    - (UIViewController *)findCurrentViewController{
        UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
        while (1){
            if (vc.presentedViewController) {
                vc = vc.presentedViewController;
            } else if ([vc isKindOfClass:[UITabBarController class]]) {
                vc = ((UITabBarController*)vc).selectedViewController;
            } else if ([vc isKindOfClass:[UINavigationController class]]) {
                vc = ((UINavigationController*)vc).visibleViewController;
            } else {
                break;
            }
        }
        return vc;
    }
    

    如何在多次presentViewController后直接返回到指定层

    UIViewController *controller = self;
        while(controller.presentingViewController != nil){
            controller = controller.presentingViewController;
        }
        [controller dismissViewControllerAnimated:YES completion:nil];
    

    push之后时删除当前页面

     NSMutableArray *controllers = [[self.navigationController viewControllers]mutableCopy];
                   for (UIViewController *vc in controllers) {
                                 if ([vc isKindOfClass:[@"xxx" class]]) {
                                     [controllers removeObject:vc];
                                     break;
                                 }
                             }
     [controllers addObject:vc];
     self.navigationController. viewControllers=controllers;
    

    相关文章

      网友评论

          本文标题:iOS事件响应链

          本文链接:https://www.haomeiwen.com/subject/qfxmhktx.html