我正在学习Objective-C,我目前正在尝试在视图控制器之间进行转换,适用于iOS4 / iOS5设备(之后我会用故事板做同样的iOS5)。
据我所知,你只能在ViewControllers&当viewDidUnload
在屏幕外时,您使用viewController.view
释放内存。
我发现的示例确实实现了在buttonIsClicked时调用的方法中的下一个视图控制器。但是,让我们说我想回去&第四,我很确定这不是正确的方法(即,如果我想保留表格值等)。
所以我把所有的viewControllers都给了我的appDelegate,然后我想把它们存放在一个很好的包含NSMutableDictionnary中,这样我就可以轻松地使用以下方法检索一个实例:
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
UIViewController *next = [appDelegate.controllers objectForKey:@"RegisterViewController"];
但是......它确实打破了一切(获得空白视图)。
我很想明白为什么。
这不起作用:
[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];
但这会:
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];
答案 0 :(得分:1)
似乎你没有立即发表你的字典 - 它仍然是零并且忽略了所有操作本身。在使用之前你必须先对它进行alloc-init。