injecting UINibExternalObjects to an self initialzied ViewController
I want to navigate from one ViewController to another. As part of this I want to pass the ViewController I want to navigate to some information. I encapsulated the information into an Object that I want to hook up as an external object with the target viewController.
I created the external Object insi开发者_如何学Cde IB gave it the identifier I referenced in the NSDictionary that is passed to the NibLoading method.
NSArray* topLevelObjs = nil;
NSMutableDictionary* options = [[NSMutableDictionary alloc] initWithCapacity:1];
NSMutableDictionary* config = [[NSMutableDictionary alloc] initWithCapacity:1];
id detailImageVC = [[SelectedImageModalViewController alloc] init];
SelectedImageModalModel* selectImageModalModel = [[SelectedImageModalModel alloc] init];
selectImageModalModel.imageName = @"img@2x.png";
[config setValue:selectImageModalModel forKey:@"SelectImageModalModel"];
[options setValue:config forKey:UINibExternalObjects];
topLevelObjs = [[NSBundle mainBundle] loadNibNamed:@"SelectedImageModalViewController" owner:detailImageVC options:options];
if ([topLevelObjs count] == 0)
{
NSLog(@"Warning! Could not substitute proxy objects in xib file.\n");
return;
}
[appDelegate.navigationController presentModalViewController:detailImageVC animated:YES];
[options release];
[config release];
[selectImageModalModel release];
[detailImageVC release];
What I expected was, that after I called presentModalViewController:animated: I would receive a call to viewDidLoad on the very same detailImageVC with my external objects connected.
Instead neither happens. viewWillApear: will get called, but the detailImageVC won't hold my external reference.
Any idea, hind or comment is appreciated. Thanks!
viewDidLoad
will be called if only ViewController loaded the view itself.
For example; initWithNibName
does not load the view, it just sets the nib name. When ViewController needs its view in some future point and if there is no view in ViewController.view
then ViewController will load view like you did AND THEN INVOKE viewDidLoad
itself.
Your code loads the view of ViewController itself. So you should call the viewDidLoad
method in your code like this:
topLevelObjs = [[NSBundle mainBundle] loadNibNamed:@"SelectedImageModalViewController" owner:detailImageVC options:options];
if (topLevelObjs.count == 0) {
NSLog(@"Warning! Could not substitute proxy objects in xib file.\n");
return;
} else {
[detailImageVC viewDidLoad];
}
If your detailImageVC
does not hold your external object then you should check your nib file for IBOutlet
bindings and your SelectedImageModalViewController
for corresponding @property
. If property is not strong like @property(nonatomic, strong)
on ARC, or, is not reatin on Non-ARC like @property(nonatomic, retain)
then it will not hold your object after awaking from nib.
精彩评论