如何获得根视图控制器?

我需要一个根视图控制器的实例。 但是这个代码返回null:

UIViewController *rootViewController = (UIViewController*)[[[UIApplication sharedApplication] keyWindow] rootViewController]; 

任何想法为什么?

另外当我尝试获取控制器的数组:

 NSArray *viewControllers = self.navigationController.viewControllers; 

这只返回一个控制器,但是这个返回不是我的根视图控制器。

如果我试图调用这个:

 UIViewController *root = (UIViewController*)[self.navigationController.viewControllers objectAtIndex:0]; 

这仍然返回null。

还有什么我可以尝试获得我的根视图控制器的实例?

谢谢。

9 Solutions collect form web for “如何获得根视图控制器?”

如果您试图访问您在rootViewController设置的rootViewController 。 尝试这个:

Objective-C的

 YourViewController *rootController = (YourViewController*)[[(YourAppDelegate*) [[UIApplication sharedApplication]delegate] window] rootViewController]; 

迅速

 let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate let viewController = appDelegate.window!.rootViewController as YourViewController 

Swift 3

 let appDelegate = UIApplication.shared.delegate as! AppDelegate let viewController = appDelegate.window!.rootViewController as! YourViewController 

简而言之就是访问根视图控制器。

目标C

 UIViewController *controller = [UIApplication sharedApplication].keyWindow.rootViewController; 

Swift 2.0

 let viewController = UIApplication.sharedApplication().keyWindow?.rootViewController 

正如@ 0x7fffffff 这里所build议的那样,如果你有UINavigationController,可以很容易做到:

 YourViewController *rootController = (YourViewController *) [self.navigationController.viewControllers objectAtIndex: 0]; 

上面的答案中的代码返回UINavigation控制器(如果你有它),如果这是你所需要的,你可以使用self.navigationController

Objective-C格式:

 UIViewController *objViewController = [UIApplication sharedApplication].keyWindow.rootViewController; 

Swift 2格式:

 let objViewController = UIApplication.sharedApplication().keyWindow?.rootViewController 

Swift 3和4格式:

 let objViewController = UIApplication.shared.keyWindow?.rootViewController 

除非你有一个很好的理由,否则在你的根控制器中这样做:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTheEvent:) name:@"ABCMyEvent" object:nil]; 

而当你想通知它:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"ABCMyEvent" object:self]; 

迅速做到这一点,你可以从任何地方调用它,它返回可选,所以要注意:

 /// EZSwiftExtensions - Gives you the VC on top so you can easily push your popups var topMostVC: UIViewController? { var presentedVC = UIApplication.sharedApplication().keyWindow?.rootViewController while let pVC = presentedVC?.presentedViewController { presentedVC = pVC } if presentedVC == nil { print("EZSwiftExtensions Error: You don't have any views set. You may be calling them in viewDidLoad. Try viewDidAppear instead.") } return presentedVC } 

它作为标准function包含在:

https://github.com/goktugyil/EZSwiftExtensions

Swift 3:Chage ViewController withOut Segue并发送AnyObject使用:在目标ViewController上使用Identity MainPageViewController

  let mainPage = self.storyboard?.instantiateViewController(withIdentifier: "MainPageViewController") as! MainPageViewController var mainPageNav = UINavigationController(rootViewController: mainPage) self.present(mainPageNav, animated: true, completion: nil) 

或者如果您想更改视图控制器并发送数据

让mainPage = self.storyboard?.instantiateViewController(withIdentifier:“MainPageViewController”)as! MainPageViewController

让dataToSend =“ 任何string ”或var ObjectToSend: AnyObject

mainPage.getData = dataToSend

  var mainPageNav = UINavigationController(rootViewController: mainPage) self.present(mainPageNav, animated: true, completion: nil) 

Swift 3:

 let appDelegate = UIApplication.shared.delegate as! AppDelegate let rootViewController = appDelegate.window!.rootViewController as! YourViewController 

Swift 3

let rootViewController = UIApplication.shared.keyWindow?.rootViewController

  • 呈现和解除模态视图控制器
  • 检查视图控制器是以模态方式呈现,还是推送到导航堆栈上
  • 在iOS中出现键盘时向上移动UIView
  • 如何禁用UIPageViewController的轻扫手势?
  • Swift,UIView控制器中的触摸事件
  • 期待了解iOS UIViewController生命周期
  • 从导航堆栈中删除viewcontrollers
  • View Controller Containment如何在iOS 5中工作?
  • 容器视图被推下来,就像它有一个UINavigationBar?
  • 什么时候应该使用initWithNibName初始化视图控制器?
  • 如何以编程方式在UINavigationController中按“返回”button