可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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
回答2:
In short hand way of access root view controller.
Objective C
UIViewController *controller = [UIApplication sharedApplication].keyWindow.rootViewController;
Swift 2.0
let viewController = UIApplication.sharedApplication().keyWindow?.rootViewController
回答3:
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
回答4:
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
.
回答5:
Unless you have a good reason, in your root controller do this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onTheEvent:)
name:@"ABCMyEvent"
object:nil];
And when you want to notify it:
[[NSNotificationCenter defaultCenter] postNotificationName:@"ABCMyEvent"
object:self];
回答6:
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
回答7:
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)
回答8:
Swift 3
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
回答9:
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)