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