1.的iOS 4中引入的RootViewController的屬性上的UIWindow:
[UIApplication sharedApplication].keyWindow.rootViewController;
你需要自己設(shè)置它叉瘩,你雖然創(chuàng)建視圖控制器之后权埠。
2.我認為你需要接受的答案
+ (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
3.(誰離開了標(biāo)簽欄控制器,同時穿越)填帽,這里是我的版本返回當(dāng)前可見視圖控制器:
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
4.:
UIViewController *_topMostController(UIViewController *cont) {
UIViewController *topController = cont;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
if ([topController isKindOfClass:[UINavigationController class]]) {
UIViewController *visible = ((UINavigationController *)topController).visibleViewController;
if (visible) {
topController = visible;
}
}
return (topController != cont ? topController : nil);
}
UIViewController *topMostController() {
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *next = nil;
while ((next = _topMostController(topController)) != nil) {
topController = next;
}
return topController;
}
_topMostController(UIViewController *cont)是一個輔助函數(shù)砰粹。 現(xiàn)在唧躲,所有你需要做的就是調(diào)用topMostController()而最上面的的UIViewController應(yīng)返還!
5.@UIWindow中(擴展)-(的UIViewController *)topMostController{ 的UIViewController * topController=[個體經(jīng)營RootViewController的]; 而(topController.presentedViewController){ topController=topController.presentedViewController; } 返回topController;}@結(jié)束
6.這個偉大的工程從任何根視圖微機控制找到了頂視圖控制器
+ (UIViewController *)topViewControllerFor:(UIViewController *)viewController
{
if(!viewController.presentedViewController)
return viewController;
return [MF5AppDelegate topViewControllerFor:viewController.presentedViewController];
}
/* View Controller for Visible View */
AppDelegate *app = [UIApplication sharedApplication].delegate;
UIViewController *visibleViewController = [AppDelegate topViewControllerFor:app.window.rootViewController];
7.我最近買了這個情況在我的一個項目碱璃,該項目所需的任何顯示的顯示控制器是和什么是類型(的UINavigationController CodeGo.net弄痹,經(jīng)典控制器或自定義視圖控制器)的通知視圖,修改厘贼。 所以我JUSTE發(fā)布我的代碼界酒,這是很容易的,實際上是基于一個協(xié)議嘴秸,它是靈活毁欣,每一種類型的容器控制器。它似乎與最后的答案是相關(guān)的岳掐,但在一個更靈活的方式凭疮。 你可以在這里搶碼:PPTopMostController 并得到了最頂端
UIViewController *c = [UIViewController topMostController];
8.找出哪些是特定視圖的最上面的視圖可以很容易做到通過導(dǎo)航向上的視圖/超景層次:
UIView* view = myView;
while (view.superview != nil)
view = view.superview;
對于視圖控制器的東西不是那么容易,因為沒有概念串述,作為一個“父控制器”执解。無論如何,一個解決方案可以通過利用一個事實,即控制器鏈接在一起的響應(yīng)鏈被發(fā)現(xiàn)衰腌,所以你可以瀏覽那一個:
-(UIViewController*)topMostControllerOf:(UIView*)view {
id nextResponder = nil;
id v = view;
while (nextResponder = [v nextResponder]) {
if ([nextResponder isKindOfClass:[YOURCONTROLLERCLASS class]])
return nextResponder;
v = nextResponder;
}
return nil;
}
請注意在線路增加了可選的檢查if ([nextResponder isKindOfClass:[YOURCONTROLLERCLASS class]])考慮到一個事實新蟆,即在響應(yīng)鏈中可能不會導(dǎo)致你你想到哪里去,所以更好右蕊,如果你停止搜索點在哪里你知道這是確定的琼稻。 在你的情況,YOURCONTROLLERCLASS可能的UINavigationController或任何特定的控制器你正在尋找饶囚。