朋友介绍的 很好用
.h
#import
@interface HXGetTheViewController : NSObject
+ (UIViewController *)getNowViewController;
@end
.m
+ (UIViewController *)getNowViewController {
UIViewController * getVc = [UIApplication sharedApplication].keyWindow.rootViewController;
while(1) {
//根据不同的页面切换方式,逐步取得最上层的viewController
if ([getVc isKindOfClass:[UITabBarController class]]) {
getVc = ((UITabBarController *)getVc).selectedViewController;
}
if ([getVc isKindOfClass:[UINavigationController class]]) {
getVc = ((UINavigationController *)getVc).visibleViewController;
}
if (getVc.presentedViewController) {
getVc = getVc.presentedViewController;
}else{
break;
}
}
returngetVc;
}
调用
FindPasswordViewController *joinVC = [FindPasswordViewController new];
UIViewController *getVC = [HXGetTheViewController getNowViewController];
joinVC.type=1;
[getVC.navigationController pushViewController:joinVC animated:YES];