前言
在ios開發(fā)敲茄,我們經常需要push或者present推出一個新頁面,一般是使用self.navgationController在Viewcontroller里面調用。但是很多時候,我們需要在view中做push或者present操作。這時候谱俭,有三種方法可以實現(xiàn)。
1.block
在view中設置block
typedef void (^backBlock)();
@property (nonatomic,copy) backBlock block;
然后在view中調用block宵蛀。
最后在控制器實現(xiàn)block中的push或者present方法昆著。
2.delegate
-設置view的delegate為控制器,然后在協(xié)議方法中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];