I can’t figure out what am I doing wrong here, I have there are other posts about it but they seem related to a more articulate case, in my situation, everything seems very simple, but still it doesn’t work.
In my AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MyViewController *vc = [[MyViewController alloc] init];
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:vc];
// Instantiate the window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navigationController;
// Show the window
[self.window makeKeyAndVisible];
return YES;
}
In MyViewController.m (there is no xib file)
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"self.navigationController %@", self.navigationController);
}
The result of this is:
2012-02-26 22:41:19.366 Test [4488:15203] self.navigationController (null)
EDIT
So, I almost figured out but I haven’t understood the reason.
First of all, to better specify, my view controllers were UITableViewControllers, and the problem apparently relies on the initWithStyle method customization I made:
- (id)init
{
self = [self initWithStyle:UITableViewStyleGrouped];
return self;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
[[self tableView] setBackgroundColor:kTableViewBackgroundColor];
[[self tableView] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
}
return self;
}
If I comment out the two rows inside the if, i.e. if I do:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
//[[self tableView] setBackgroundColor:kTableViewBackgroundColor];
//[[self tableView] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
}
return self;
}
the self.NavigationController gets instantiated properly, otherwise it doesn’t. Can someone explain me why this happens?
When you override the
initmethod, it basically means the properties of the super class view controller accessor method, which has the property the navigationController is set to nil, and you have thetableViewproperty instantiated instead.You can set the tableview style and other properties in the
viewDidLoad