1德挣、獲取當(dāng)前UIViewController
1.1項(xiàng)目使用 UINavigationController --> UIViewController 結(jié)構(gòu)
+ (UIViewController *)getCurViewController {
UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
NSLog(@"當(dāng)前ViewController:%@",nav.visibleViewController.class);
return nav.visibleViewController;
}
1.2項(xiàng)目使用 UITabBarController --> UINavigationController --> UIViewController 結(jié)構(gòu)
+ (UIViewController *)getCurViewController {
UITabBarController *tabBarController = ((UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController);
UINavigationController *nav = tabBarController.selectedViewController;
NSLog(@"當(dāng)前ViewController:%@",nav.visibleViewController.class);
return nav.visibleViewController;
}
1.3獲取present出來的ViewController
+ (UIViewController *)getPresentedViewController {
UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *presentedVC = rootVC;
if (presentedVC.presentedViewController) {
presentedVC = presentedVC.presentedViewController;
}
return presentedVC;
}
2、通過UIView獲取所在的UIViewController
- (UIViewController *)viewController {
UIView *next = self;
while ((next = [next superview])) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
在自定義UIView中實(shí)現(xiàn)頁面跳轉(zhuǎn)等操作
[[self viewController].navigationController pushViewController:nextViewController animated:YES];
[[self viewController].navigationController popViewControllerAnimated:YES];//返回上一個(gè)頁面
如有疑問或錯(cuò)誤的地方,歡迎留言唤蔗!
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^