How to get root view controller?

2019-01-13 05:09发布

I need an instance of root view controller.

I tried those 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 isn't my root view controller.

If I try to take from navigation controller:

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

Returns: null:

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

Thanks.

9条回答
Ridiculous、
2楼-- · 2019-01-13 05:29

if you are trying to access the rootViewController you set in your 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

let viewController = UIApplication.shared.keyWindow!.rootViewController as! YourViewController
查看更多
冷血范
3楼-- · 2019-01-13 05:30

Swift way to do it, you can call this from anywhere, it returns optional so watch out about that:

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

Its included as a standard function in:

https://github.com/goktugyil/EZSwiftExtensions

查看更多
该账号已被封号
4楼-- · 2019-01-13 05:30

Swift 3: Chage ViewController withOut Segue and send AnyObject Use: Identity MainPageViewController on 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 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)  
查看更多
Ridiculous、
5楼-- · 2019-01-13 05:31

As suggested here by @0x7fffffff, if you have UINavigationController it can be easier to do:

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

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

查看更多
\"骚年 ilove
6楼-- · 2019-01-13 05:33

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
查看更多
ら.Afraid
7楼-- · 2019-01-13 05:36
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)
查看更多
登录 后发表回答