解決類似網易新聞客戶端收到新聞推送後,彈出一個UIAlert,然後跳轉到新聞詳情頁面這種需求 1.提供一個UIView的分類方法,這個方法通過響應者鏈條獲取view所在的控制器 - (UIViewController *)parentController { UIResponder *responder = [self nextResponder]; while (responder) { if ([responder isKindOfClass:[UIViewController class]]) { return (UIViewController *)responder; } responder = [responder nextResponder]; } return nil; } 2.通過控制器的布局視圖可以獲取到控制器實例對象 modal的展現方式需要取到控制器的根視圖 + (UIViewController *)currentViewController { UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; // modal展現方式的底層視圖不同 // 取到第一層時,取到的是UITransitionView,通過這個view拿不到控制器 UIView *firstView = [keyWindow.subviews firstObject]; UIView *secondView = [firstView.subviews firstObject]; UIViewController *vc = secondView.parentController; if ([vc isKindOfClass:[UITabBarController class]]) { UITabBarController *tab = (UITabBarController *)vc; if ([tab.selectedViewController isKindOfClass:[UINavigationController class]]) { UINavigationController *nav = (UINavigationController *)tab.selectedViewController; return [nav.viewControllers lastObject]; } else { return tab.selectedViewController; } } else if ([vc isKindOfClass:[UINavigationController class]]) { UINavigationController *nav = (UINavigationController *)vc; return [nav.viewControllers lastObject]; } else { return vc; } return nil; }