您好,欢迎来到知库网。
搜索
您的当前位置:首页iOS开发-获取View所在的Viewcontroller

iOS开发-获取View所在的Viewcontroller

来源:知库网

通常使用self.navgationController是在Viewcontroller里面调用的,但是有的时候需要在view上也需要调用push或者present推出新的页面的方法,那么可以使用以下三种。
1、block方法。
2、代理方法。
3、获取该View所在的Viewcontroller。【这个也是我要说的方法】

//获取View所在的Viewcontroller方法

- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
//使用方法:
[[self viewController].navigationController pushViewController:[yourViewController new]animated:YES];

Copyright © 2019- zicool.com 版权所有 湘ICP备2023022495号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务