2014-06-14 54 views

回答

17

您可以使用下面的代碼中訪問它,如果rootViewControllerUIViewController

UIViewController *rootController=(UIViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController; 

但如果它是一個UINavigationController您可以使用下面的代碼。

UINavigationController *nav=(UINavigationController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController; 
UIViewController *rootController=(UIViewController *)[nav.viewControllers objectAtIndex:0]; 
1

斯威夫特:

let rootVC = self.window?.rootViewController

+0

什麼是'self'在這種情況下? – sasquatch

+0

讓rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController –

+0

對不起,簡直就是AppDelegate。 – ingconti

0

從控制檯:

po [[[UIApplication sharedApplication] keyWindow] rootViewController] <UINavigationController: 0x15f076c00>