jenjenjen
jenjenjen

Reputation: 29

How To Stop Reloading ViewController after First Launch

Is there any way to stop reloading view controller, even the button of push segue have been clicked? Due to the view controller might have some program processing and it might takes time, and i don't want to reload that view again and restart the program again when the user clicked navigated to that view.

Thanks a lot for helping me out.

Upvotes: 0

Views: 1151

Answers (1)

nhgrif
nhgrif

Reputation: 62062

Any code that needs to run only when the app is launched can be put in appDidFinishLaunching:withOptions:.

If you have code that needs to be run as set up code only the first very time the app is launched and then never run again so long as the app remains installed on the device, you can still use appDidFinishLaunching:withOptions:, but you'll need to put some logic in to determine whether or not the app has been launched before. The easiest way of accomplishing that looks something like this:

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"previouslyLaunched"]) {
    // app already launched
} else {
    [[NSUserDefaults standardUserDefaults] setBool:YES 
        forKey:@"previouslyLaunched"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // This is the first launch ever
}

Edit: You could apply the above code snippet to individual view controllers as well.


But as a note, the viewDidLoad method of a view controller is not necessarily called every time the view is presented. Once viewDidLoad is called and the view is put onto the navigation stack, viewDidLoad won't be called again unless the view is removed from the navigation stack and then needed to be put back on the navigation stack.

For example, when using a navigation controller, the first time the first view is presented, the viewDidLoad, viewWillAppear, and viewDidAppear methods are all called. When you navigate to the next view in the navigation controller, then go back to the first view, viewWillAppear and viewDidAppear will be called again, but viewDidLoad will not be called again.

Upvotes: 2

Related Questions