zoukankan      html  css  js  c++  java
  • iOS

    当界面比较复杂时有时会将一个view单独抽取出来作为一个单独的类.但当涉及到控制器的跳转的时候就不得不用代理或者block回调来去父容器的控制器来进行跳转,很不方便.不过发现一个黑科技如下.

    • 创建TestViewRed测试view的类
    • TestViewRed.h
    
    #import <UIKit/UIKit.h>
    
    @interface TestViewRed : UIView
    
    @end
    
    
    • TestViewRed.m
    #import "TestViewRed.h"
    #import "ViewController.h"
    #define KScreen_Bounds [UIScreen mainScreen].bounds
    #define KScreen_Size [UIScreen mainScreen].bounds.size
    #define KScreen_Width [UIScreen mainScreen].bounds.size.width
    #define KScreen_Height [UIScreen mainScreen].bounds.size.height
    
    @interface TestViewRed ()
    @property (strong, nonatomic) UIView * view;
    @property (strong, nonatomic) UIViewController * vc;
    @end
    
    @implementation TestViewRed
    
    
    -(instancetype)init{
    
        if (self = [super init]) {
            self.view = [[UIView alloc] init];
            self.view.backgroundColor = [UIColor redColor];
            self.view.userInteractionEnabled = YES;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickRedView)];
            [self.view addGestureRecognizer:tap];
            [self addSubview:self.view];
        }
        return self;
    }
    
    //给红色view添加轻触手势
    -(void)clickRedView{
    
        UIViewController * vc = [UIViewController new];
        ViewController * vcOne = (ViewController *)[self View:self.view];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        [vc.view addGestureRecognizer:tap];
        self.vc = vc;
        
        vc.view.backgroundColor = [UIColor whiteColor];
        [vcOne presentViewController:vc animated:YES completion:nil];
    }
    
    //测试弹出的控制器的dismiss方法
    -(void)dismiss{
    
        [self.vc dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
    
    //重新布局self.view因为如果在init方法中布局self.view则self.frame 都是 0
    -(void)layoutSubviews{
    
        self.view.frame = self.frame;
    }
    //可以获取到父容器的控制器的方法,就是这个黑科技.
    - (UIViewController *)View:(UIView *)view{
        UIResponder *responder = view;
        //循环获取下一个响应者,直到响应者是一个UIViewController类的一个对象为止,然后返回该对象.
        while ((responder = [responder nextResponder])) {
            if ([responder isKindOfClass:[UIViewController class]]) {
                return (UIViewController *)responder;
            }
        }
        return nil;
    }
    
    • ViewControllerviewDidLoad添加如下代码
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        TestViewRed * view = [[TestViewRed alloc] init];
        view.frame = CGRectMake(0, 0, KScreen_Width, 360);
        view.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:view];
    }
    
    • 测试效果如下
  • 相关阅读:
    Winform控件Enable=false显示优化
    request 报错The remote server returned an error: (415) Unsupported Media Type.
    InvalidArgument=Value of '1' is not valid for 'index'
    Redis学习笔记#12 Redis Cluster 集群
    centos7安装docker
    Redis学习笔记#11 关于key的建议
    Redis学习笔记#10 lua脚本,整合springboot调用
    ActiveMQ学习笔记#1
    SpringBoot学习笔记#2 具体化配置文件
    SpringBoot学习笔记#1 创建一个RESTful Web服务
  • 原文地址:https://www.cnblogs.com/adampei-bobo/p/6742509.html
Copyright © 2011-2022 走看看