问题
I am presenting a modal view controller. In that modal view controller's viewDidLoad
, I can't seem to find any reference to the parent VC.... parentViewController
, presentingViewController
, etc. If this isn't set yet, how am I supposed to get a reference to it?
回答1:
For a general solution, you could have your own property in the second controller which is a weak
pointer to the first view controller. The first controller should set the second controller's property in the first segue's prepareForSegue
.
In answer to your question, parentViewController
is only set when using view controller containment (which you're not doing). The presentingViewController
is set when you do presentViewController
, but the behavior changes depending upon whether the first view controller is embedded in a navigation controller or not (if it is embedded, the second view controller's presentingViewController
is the navigation controller, if not, it is the view controller you'd expect it to be). And it appears that presentingViewController
is not set at all if you do pushViewController
.
Bottom line, I find it to be far more reliable and consistent if you set a custom property of the second controller during the prepareForSegue
in the first controller.
So, in the SecondViewController
, I have a .h @interface
that looks like:
@class FirstViewController;
@interface SecondViewController : UIViewController
@property (nonatomic, weak) FirstViewController *firstViewController;
@end
And then in the first view controller, I have a prepareForSegue
that looks like:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"IdentifierForSegueToSecondControllerHere"])
{
SecondViewController *controller = segue.destinationViewController;
controller.firstViewController = self;
}
}
Obviously, that above point applies if you're using storyboards. If you're using NIBs, you would do something like:
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
controller.firstViewController = self;
[self presentViewController:controller animated:YES completion:nil];
Anyway, once you've set that firstViewController
property in your SecondViewController
, that SecondViewController
can now access properties or invoke public methods of the FirstViewController
, such as:
// up at the top, where you have your import statements, include the following
#import "FirstViewController.h"
// then, in the @implementation of the SecondViewController, you can reference
// the first view controller and its properties, such as:
self.firstViewController.someStringProperty = @"some value";
回答2:
Conceptually speaking, a view can be loaded at anytime. Just because your view has been loaded doesn't necessarily mean that it's about to be displayed in some form.
The earliest point at which you can discover your presenting view controller is in the viewWillAppear:
method of UIViewController
.
来源:https://stackoverflow.com/questions/13898324/when-is-the-parent-presenting-view-controller-set-on-a-modal-view-controller