Problem with ViewDidLoad method

匆匆过客 提交于 2019-12-25 05:12:22

问题


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;
}


来源:https://stackoverflow.com/questions/6461624/problem-with-viewdidload-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!