How to get the root controller?

I need an instance of the root view controller.

I tried these approaches:

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

Returns: null :

Also, when I try to get an array of controllers:

 NSArray *viewControllers = self.navigationController.viewControllers; 

It returns only one controller, but it is not my root view controller.

If I try to take from the navigation controller:

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

Returns: null :

Any ideas why? What else can I try to get an instance of my root view controller?

Thank you

+96
objective-c uiviewcontroller ios5 swift uinavigationcontroller
Sep 14
source share
10 answers

if you are trying to access the rootViewController you installed in appDelegate. try this:

Objective-c

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

swift

 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 

Swift 4 & 4.2

 let viewController = UIApplication.shared.keyWindow!.rootViewController as! YourViewController 
+174
Sep 14 '12 at 5:17
source share

In manual mode, access to the root view controller.

Goal c

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

Swift 2.0

 let viewController = UIApplication.sharedApplication().keyWindow?.rootViewController 
+34
Apr 27 '16 at 3:45
source share

Objective-C format:

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

Swift 2 format:

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

Swift 3 and 4 format:

 let objViewController = UIApplication.shared.keyWindow?.rootViewController 
+17
Jul 31 '16 at 9:28
source share

As suggested here with @ 0x7fffffff, if you have a UINavigationController, this might be easier to do:

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

The code in the answer above returns the UINavigation controller (if you have one), and if that is what you need, you can use self.navigationController .

+10
May 22 '13 at 22:18
source share

If you do not have a good reason, in the root controller, do the following:

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

And if you want to notify about it:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"ABCMyEvent" object:self]; 
+5
Feb 16 '14 at 10:31
source share

A quick way to do this, you can call it from anywhere, it comes back optionally, so watch out for this:

 /// 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 } 

It is included as a standard feature in:

https://github.com/goktugyil/EZSwiftExtensions

+1
Nov 24 '15 at 4:35
source share

Swift 3: Chage ViewController withOut Segue and send AnyObject Usage: Identity MainPageViewController in the target ViewController

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

, or if you want to change the view controller and send data

 let mainPage = self.storyboard?.instantiateViewController(withIdentifier: "MainPageViewController") as! MainPageViewController let dataToSend = "**Any String**" or var ObjectToSend:**AnyObject** mainPage.getData = dataToSend var mainPageNav = UINavigationController(rootViewController: mainPage) self.present(mainPageNav, animated: true, completion: nil) 
+1
Sep 21 '16 at 9:03
source share

Swift 3

let rootViewController = UIApplication.shared.keyWindow?.rootViewController

+1
Apr 20 '17 at 23:58 on
source share

Swift 3:

 let appDelegate = UIApplication.shared.delegate as! AppDelegate let rootViewController = appDelegate.window!.rootViewController as! YourViewController 
0
Nov 25 '16 at 10:34
source share
 let view:UIView = UIView() view.frame = CGRect(x:0, y:0, width:UIApplication.shared.statusBarFrame.width, height:UIApplication.shared.statusBarFrame.height view.backgroundColor = UIColor.init(named: "yourColor") UIApplication.shared.keyWindow!.rootViewController?.view.addSubview(view) 
0
Jul 21 '18 at 15:20
source share



All Articles