iOS View上调用push或present跳转方法

前言

在ios开发,我们经常需要push或者present推出一个新页面,一般是使用self.navgationController在Viewcontroller里面调用。但是很多时候,我们需要在view中做push或者present操作。这时候,有三种方法可以实现。
1.block
在view中设置block

typedef void (^backBlock)();
@property (nonatomic,copy) backBlock block;

然后在view中调用block。
最后在控制器实现block中的push或者present方法。
2.delegate
-设置view的delegate为控制器,然后在协议方法中push或者present。
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];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容