Being new to Cocoa, I'm having a few issues with Interface Builder
, UIViewController
and friends.
I have a UIViewController
subclass with a UIView
defined in a xib, and with the controller's view outlet connected to the view. The xib's "file's owner" is set as myViewcontroller subclass.
In this one instance, the following code to load the controller/view (from the main view controller) doesn't work as expected:
if ( self.myViewController == nil )
{
self.myViewController = [[MyViewController alloc]
initWithNibName:@"MyViewController" bundle:nil];
}
[self.navigationController
pushViewController:self.myViewController animated:YES];
In MyViewController's methods, I have placed breakpoints and log messages to see what is going on:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
NSLog(@"initWithNibName\n");
}
return self;
}
-(void)viewDidLoad {
[super viewDidLoad];
NSLog(@"viewDidLoad\n");
}
Expected result
Both -initWithNibName
and -viewDidLoad
methods are called, and myViewController's view is displayed.
Observed result
Only -initWithNibName
is called, the view is not displayed.
Have I missed something? Can anyone recommend anything to check? (Particularly in the wondrously opaque Interface Builder tool).
Ok, I have a partial answer - maybe the gurus can explain some more. The problem is:
[self.navigationController pushViewController:myViewController animated:YES];
Looking more closely, in this case self.navigationController
is nil - so the push message is going no-where.
Instead, if I send:
[self.view addSubview:self.myViewController.view];
Then the view appears and -viewDidLoad
is called.
I'm not entirely sure why self.navigationController
is not set in this instance - the only thing I can think of is that self
is a subclass of UIViewController
rather than UITableViewController
(where the pushViewController
code came from).
Also, silently allowing messages to go to nil seems like a bad idea, although these answers say otherwise. See also my question here.
Final edit:
Answers in comments below, I've realised the display function that I was actually after (given myViewController is modal) is:
[self presentModalViewController:myViewController animated:YES];
Thanks everyone for their helpful responses.
Don't badmouth silent-message-to-nil, you'll use it all the time when you get used to it :-) It can certainly be mysterious when you first start out. As for why navigationController is nil: is it being initialized as the root view controller of a UINavigationController? If not, you can't use pushViewController until you do that!
I think Adam has it exactly here, make sure your view is properly placed into a navigation stack before trying to get its navigationController.