Problem with ViewDidLoad method

2019-08-29 07:05发布

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条回答
\"骚年 ilove
2楼-- · 2019-08-29 07:50

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;
}
查看更多
登录 后发表回答