如何在iOS故事板中获取根视图控制器

7

我在故事板的根视图中有一个菜单表,我希望每当子视图更新数据时重新加载它。如何获取对根视图的引用,以便我可以调用其reloadData方法?

3个回答

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

Swift:

let rootVC = self.window?.rootViewController

中文:

let rootVC = self.window?.rootViewController


在这个上下文中,“self”是什么? - saurabh
让rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController - Naveen Shan
抱歉,只是在AppDelegate里。 - ingconti

0
从控制台: po [[[UIApplication sharedApplication] keyWindow] rootViewController] <UINavigationController: 0x15f076c00>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接