user4363124
user4363124

Reputation: 121

How do I make a viewController appear when app loads?

I've got a working app that doesn't crash and I've set my initial VC to be the one I want. When running on the simulator, my app opens for the first time on the VC I want, but if I go to; Hardware -> Home and click on the app again on the main iPhone menu, the app loads the VC I left with and not the VC I set an my main. I'm new to iOS development so I don't understand what's not working. Other questions's answers don't help me a bit since my main VC is set on my storyboard. Thanks for the help guys! This is in my appDelegate file:

func applicationWillResignActive(application: UIApplication) {

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"appWillResignActive", name:UIApplicationWillResignActiveNotification, object: nil)

    func appWillResignActive() -> () {
        self.navigationController?.popToViewController(ViewController, animated: false)
    }
}

Upvotes: 0

Views: 169

Answers (1)

Armin
Armin

Reputation: 1150

You can subscribe to the UIApplicationWillResignActiveNotification and call a method right before the app goes to the background, to set the VC to the one you want. This way, once they re-open the app, it should be where you want it to be.

Obj-C:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];

-(void) appWillResignActive {
    [self.navigationController popToViewController:<UIViewController you want> animated:NO];
}

SWIFT:

NSNotificationCenter.defaultCenter().addObserver(self, selector:"appWillResignActive", name:UIApplicationWillResignActiveNotification, object: nil)

func appWillResignActive() -> () {
    self.navigationController.popToViewController(<UIViewController you want>, animated: false)
}

Upvotes: 3

Related Questions