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 *c开发者_如何转开发hild = [[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?
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.
Set it to nil (or do nothing at all). Releasing would be wrong because your property doesn't retain (it just assigns).
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:
- Use
retain
instead ofassign
in your property declaration. - Use
[self.parent release]
in yourdealloc
method in your child view controller.
精彩评论