Check it:
NSUserDefaults *defaults = [NSUserDefaults standa开发者_JAVA技巧rdUserDefaults];
NSLog(@"Checking login--user value is %@", [defaults valueForKey:@"userID"]);
if ([defaults valueForKey:@"userID"] == NULL){
LoginViewController *loginController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil];
[window addSubview:loginController.view];
[loginController release];
}
else {
[window addSubview:[navigationController view]];
}
Every other place when I put a subview into another view, I release that view after I've done that, because it's now owned by the view it's a subview of. HERE, though, when I do [loginController release]
, every IBAction on that loginController gets called against a deallocated instance. Commenting out that line makes everything work.
I note the difference in approach between my loginController and the navigationController that came with the template; the navigationController is a synthesized property that gets released in -(void)dealloc{ }
, so it's still around after being put into window
.
-addSubview:
only retains the view, not the controller.
You shouldn't manually add a controller's view to a view hierarchy as the controller won't be retained and even if retained won't receive the expected system calls (e.g. orientation changes, etc.).
Instead add the controller to a UIWindow
using its rootViewController
property.
If you still feel the need to add the controller's view somewhere, then maybe you shouldn't be using view controllers but simple custom views instead.
精彩评论