PDA

View Full Version : Parent View Controller




idelovski
May 11, 2009, 08:24 PM
I have two view controllers, one is presenting the other modally with -presentModalViewController:animated:, and the second controller has a view with a button that should close it by calling parents -dismissModalController which just cals -dismissModalViewControllerAnimated:.

The problem is that I can't use parentViewController property of second controller to set a target for the dismissing action:
[theButton addTarget:self.parentViewController
action:@selector(dismissModalController)
forControlEvents:UIControlEventTouchUpInside];

This ends with unhandled exception when I press the button.

Then I added realParentController property and -initWithRealParent: method to the second controller and when I changed the code above to use that new property, the modal view closed ok without any unhandled exceptions.
[theButton addTarget:self.realParentController
action:@selector(dismissModalController)
forControlEvents:UIControlEventTouchUpInside];

I can see in debugger while I'm in -loadView that the parentViewController property of the second controller is not nil, but why it isn't the right pointer to the parent controller?



idelovski
May 12, 2009, 05:08 AM
Well, this has been noticed by others: Is parentViewController always a Navigation controller? (http://stackoverflow.com/questions/244588/is-parentviewcontroller-always-a-navigation-controller)

So it's a known issue, not a bug, almost a feature!

:D

Yet, Apple says something else: The underlying view controller if this view controller is a modal view controller; otherwise, the enclosing navigation or tab bar controller. ( http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instp/UIViewController/parentViewController)