Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Accessing AppDelegate window from viewController

I make walkthrough (onboarding flow) in my app and I'd like to have a skip button. The button is located on viewController, so I figured out that the best way to move to another viewController would be access app delegate window.

However, it keeps getting me an error that AppDelegate.Type does not have a member called "window".

@IBAction func skipWalkthrough(sender: AnyObject) {     let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate     AppDelegate.window!.rootViewController = RootViewController    } 

Is there anything wrong with such approach?

Thanks in advance!

like image 746
theDC Avatar asked May 04 '15 10:05

theDC


People also ask

What is use of AppDelegate in Swift?

Overview. Your app delegate object manages your app's shared behaviors. The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system.


1 Answers

You have a typo it is supposed to be appDelegate not AppDelegate. So like this:

@IBAction func skipWalkthrough(sender: AnyObject) {     let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate     appDelegate.window!.rootViewController = RootViewController    } 

Swift 3.2

@IBAction func skipWalkthrough(_ sender: AnyObject) {         let appDelegate = UIApplication.shared.delegate as! AppDelegate         appDelegate.window!.rootViewController = controller     } 
like image 57
Stefan Salatic Avatar answered Sep 18 '22 23:09

Stefan Salatic