0
votes

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?

3
if you use assign attribute, you don't need to release it in your child controllerNava Carmon
If these objects are UIViews then it's safer to use [self superview] to access the parent from the child. And you won't have to worry about who retains whom.tidwall
actually child view is inside an UIPopOverController. I cannot access it from the child directly.Joey
If your ChildViewController is being presented inside a UIPopoverController, why are you giving it a pointer back to ParentViewController ?Shaggy Frog
because ChildViewController needs to run a method in ParentViewControllerJoey

3 Answers

3
votes

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.

2
votes

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

-1
votes

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.