tags:

views:

196

answers:

2

Question is should i use properties for my view controllers?

Consider the following case:

  1. I have a view controller object in my parent class: MyViewController *myVC;

  2. I don't release this view controller in parent class's dealloc method.

  3. I use view controller like this:

    // Allocate and Initialize view controller
    myVC = [[MyViewController alloc] initWithNibName:@"newView" bundle:nil];
    
    
    // Push View Controller
    [self.navigationController pushViewController:myVC animated:YES];
    
    
    // Release memory
    [myVC release];
    myVC = nil;
    

Is there any problem with this approach?

+1  A: 

What Brad Larson said. myVC will leak unless you release it in the owning class' dealloc method. Making it a property will not affect this behavior (and where does the itemEditVC var come from?) The only reason to expose it as a property would be if external classes need access to this variable and you want to ensure proper release/retain semantics when this property is modified.

drewh
+1  A: 

In your revised code, myVC is retained by the navigation controller and will be released (and deallocated) when it is popped off the stack. If this is the behavior you are looking for, then yes this code is just fine.

If you need myVC to hang around for reuse or something else later, then move the release to your parent class's dealloc method and remove the assignment to nil.

Brad Larson