Problem with ViewDidLoad method

2019-08-29 07:32发布

问题:

I have a problem with the methoed ViewDidLoad... At the start, my application must run some code but if I load one other viewController and than I return in the main Controller the application runs again the code! I want that my app runs this code at the start but when the user returns to the main view the app does anythings!

回答1:

You could put the code you need executed in the Application Delegate and just run it at startup from there.

In your project, you should have a pair of files and they should both be name like this: yourprojectnameAppDelegate, go into the implementation one.

Then just put your code in the following method

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    //your code here

    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[Hungry_ZombiesViewController alloc] initWithNibName:@"Hungry_ZombiesViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}