0

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?

Joey
  • 85
  • 1
  • 5
  • 1
    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 Sep 11 '10 at 22:23
  • actually child view is inside an UIPopOverController. I cannot access it from the child directly. – Joey Sep 12 '10 at 00:55
  • If your `ChildViewController` is being presented inside a `UIPopoverController`, why are you giving it a pointer back to `ParentViewController` ? – Shaggy Frog Sep 12 '10 at 01:03
  • because ChildViewController needs to run a method in ParentViewController – Joey Sep 12 '10 at 01:07
  • @Joey Then you've created a circular dependency in your class design. If you refactor to use a delegate pattern, you can untangle that cycle. – Shaggy Frog Sep 12 '10 at 01:11
  • So don't use @property (nonatomic, assign) ParentController *parent; use a delegate? When is it appropriate to assign something? – Joey Sep 12 '10 at 01:30
  • You use `assign` when you don't want the object to be `retain`-ed. A delegate `@property` is a good example of when you should use `assign` instead of `retain`. You would define it like this: `@property (nonatomic, assign) id delegate;` – Shaggy Frog Sep 12 '10 at 02:29
  • if you use assign attribute, you don't need to release it in your child controller – Nava Carmon Sep 11 '10 at 22:17

3 Answers3

3

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
  • 27,575
  • 16
  • 91
  • 128
  • 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 Sep 12 '10 at 00:51
  • 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 Sep 12 '10 at 00:54
  • 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 Sep 12 '10 at 01:19
  • If your child view isn't part of a nav controller stack, then yes, you can use `[self presentModalViewController:...]` – Shaggy Frog Sep 12 '10 at 02:27
2

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

Eiko
  • 25,601
  • 15
  • 56
  • 71
-1

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
  • 8,697
  • 7
  • 43
  • 51
  • -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. Sep 12 '10 at 05:18