views:

161

answers:

1

Check it:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
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.

+3  A: 

-addSubview: only retains the view, not the controller.

KennyTM
Ah. In retrospect, that's pretty obvious. I was looking at a controllerless view, there. So there's nothing I need to do about managing retain count on the view, then, is there? I guess when it removesItselfFromSuperview, it'll drop back to 1 and then when the controller is released (I'll need to make it a property so I can release it at dealloc time, I guess) it'll go away completely. Is that right?
Dan Ray
@Dan: Yes. (If you want a controller-less view, create a UIView directly.)
KennyTM