KieranWaugh
KieranWaugh

Reputation: 21

Change initial view controller swift

I need to change the initial view controller based on whether voiceover is on or not. I have tried multiple ways but I always get the same error: "Value of type 'AppDelegate' has no member 'window'" This is what I have in my didFinishLaunchingWithOptions.

    print("voiceover: \(voiceOver)") // prints if voice over is on

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let initialViewController = storyboard.instantiateViewController(withIdentifier: "voiceoverViewController")

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()

Value of type 'AppDelegate' has no member 'window'

Upvotes: 1

Views: 2172

Answers (2)

Nidhi
Nidhi

Reputation: 774

You don't have to declare window variable manually

It is available in SceneDelegate file in your project

It will have pre defined method :

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)

Example

func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
 options connectionOptions: UIScene.ConnectionOptions) {

    if let windowScene = scene as? UIWindowScene {          
        self.window = UIWindow(windowScene: windowScene) 
        let initialViewController = 
            storyboard.instantiateViewController(withIdentifier: "voiceoverViewController")            
        self.window!.rootViewController = initialViewController
        self.window!.makeKeyAndVisible()
    }
}

Upvotes: 2

Sulthan
Sulthan

Reputation: 130102

You have to declare a window property manually:

var window: UIWindow?

UIApplicationDelegate is just a protocol, it doesn't declare any properties for you.

Upvotes: 3

Related Questions