views:

85

answers:

4

I have two views, the parent view opens a pop over that has a child view.

In child controller @property (nonatomic, assign) ParentController *parent;

In parent controller ChildController *child = [[ChildController alloc] init]; child.parent = self;

My question is in the dealloc method of the child controller, do you set self.parent = nil or release?

+2  A: 

Set it to nil (or do nothing at all). Releasing would be wrong because your property doesn't retain (it just assigns).

Eiko
A: 

if you use assign attribute, you don't need to release it in your child controller

Nava Carmon
+3  A: 

This has a bad code smell. It doesn't make much sense and I'm surprised it compiles:

ChildController *child = [[ParentController alloc] init];

And I'm not sure what you mean by "pop over" -- that term has a specific meaning now in iOS (see: Consider Using Popovers for Some Modal Tasks in the iPad Human Interface Guidelines).

Your question "in the dealloc method of the child controller, do you set self.parent = nil or release?" can't be answered properly, as it's also a bad code smell. There's no reason for a child view controller to be fiddling with any reference to a parent view controller like that.

(Although some people have answered your question while I was typing this up, I think you have some design problems that need to be acknowledged)

How are you presenting your "ChildView"? Modally? If so, your code might look something like this:

- (void)showChildView
{
    ChildViewController* childViewController = [[ChildViewController alloc] initWithNibName:@"ChildView" bundle:nil];
    childViewController.delegate = self;
    childViewController.someProperty = @"Some Value";
    [self.navigationController presentModalViewController:childViewController animated:YES];
    [childViewController release];
}

You should then create a delegate protocol with your ChildViewController class that your ParentViewController class will implement, so it knows when the ChildViewController is finished so it can deal with removing the view appropriately.

Generally, the idea of the ChildViewController needing a pointer back to the ParentViewController is a bad code smell because it sets up a circular dependency.

Shaggy Frog
+1 Good points. Just use the view/viewcontroller hierarchy.
jojaba
sorry that was a typo it should be ChildController alloc. How is using a delegate different? than a pointer? Im new to objective-c
Joey
Delegation is one of the fundamental design patterns in Cocoa. See: http://developer.apple.com/library/ios/#documentation/general/conceptual/devpedia-cocoacore/Delegation.html and http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c
Shaggy Frog
In your code sample: why did you use self.navigationController presentModalViewController? why not just self presentModalViewController. whats the difference? my parent view does have a nav bar.
Joey
If your child view isn't part of a nav controller stack, then yes, you can use `[self presentModalViewController:...]`
Shaggy Frog
A: 

Why do you need a property for the parent view controller anyway? UIViewController already contains a property called parentViewController, so you don't need to define another one.

But if you must do this, you should:

  1. Use retain instead of assign in your property declaration.
  2. Use [self.parent release] in your dealloc method in your child view controller.
Calvin L
-1 if you're going to use properties, use self.parent = nil. This works regardless of whether it's retain or assign; the property will do the "right thing".
tc.