I’m learning Objective-C, and I’m currently trying to make transitions between view controllers works for iOS4/iOS5 devices (I’ll do the same iOS5 only with storyboards after).
As I understood it, you instantiate only once you ViewControllers & you use viewDidUnload to release memory when the viewController.view is out of screen.
Examples I found did instantiate the next view controller in the method called when the buttonIsClicked. But let’s say I want to go back & forth, I’m pretty sure this is not the right way (ie. if I want to keep form values, etc.).
So I gave all my viewControllers to my appDelegate, and then I wanted to store them inside a nice contained NSMutableDictionnary so that I could retreive one instance easily using :
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
UIViewController *next = [appDelegate.controllers objectForKey:@"RegisterViewController"];
But… It does break everything (getting blank views).
I’d love to understand why.
This won’t work :
[self.controllers setObject:[[LoginViewController alloc] initWithNibName:@"LoginViewController_iPhone" bundle:nil] forKey:@"LoginViewController"];
[self.controllers setObject:[[RegisterViewController alloc] initWithNibName:@"RegisterViewController_iPhone" bundle:nil] forKey:@"RegisterViewController"];
self.window.rootViewController = [self.controllers objectForKey:@"LoginViewController"];
[self.window makeKeyAndVisible];
But this will :
UIViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController_iPhone" bundle:nil];
[self.controllers setObject:loginViewController forKey:@"LoginViewController"];
[self.controllers setObject:[[RegisterViewController alloc] initWithNibName:@"RegisterViewController_iPhone" bundle:nil] forKey:@"RegisterViewController"];
self.window.rootViewController = loginViewController;
[self.window makeKeyAndVisible];
It seems that you are not instantating your dictionary – it remains nil and ignores all operations on itself. You have to alloc-init it before using it.