朋友介紹的 很好用
.h
#import
@interface HXGetTheViewController : NSObject
+ (UIViewController *)getNowViewController;
@end
.m
+ (UIViewController *)getNowViewController {
? ? UIViewController * getVc = [UIApplication sharedApplication].keyWindow.rootViewController;
? ? while(1) {
? ? ? ? //根據(jù)不同的頁面切換方式茵典,逐步取得最上層的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;
}
調(diào)用
FindPasswordViewController *joinVC = [FindPasswordViewController new];
? ? UIViewController *getVC = [HXGetTheViewController getNowViewController];
? ? joinVC.type=1;
? ? [getVC.navigationController pushViewController:joinVC animated:YES];