我无法弄清楚我在这里做错了什么,我还有其他关于它的帖子,但它们似乎与一个更清晰的案例有关,在我的情况下,一切看起来都很简单,但它仍然没有&# 39;工作。
在我的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;
}
在MyViewController.m中(没有xib文件)
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"self.navigationController %@", self.navigationController);
}
结果是:
2012-02-26 22:41:19.366 Test [4488:15203] self.navigationController (null)
所以,我几乎想通了,但我还没有理解原因。
首先,为了更好地指定,我的视图控制器是UITableViewControllers,问题显然依赖于我所做的initWithStyle方法定制:
- (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中的两行,即如果我这样做:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
//[[self tableView] setBackgroundColor:kTableViewBackgroundColor];
//[[self tableView] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
}
return self;
}
self.NavigationController被正确实例化,否则它不会被实例化。有人可以解释一下为什么会这样吗?
答案 0 :(得分:1)
你的init方法里面有什么?你是否覆盖了这种方法?如果没有,您可能应该使用Apple Reference:
中的initWithNibName:bundle:
方法
返回一个新初始化的视图控制器,其中包含指定包中的nib文件。
来自UIViewController.h
文件:
/*
The designated initializer. If you subclass UIViewController, you must call the super implementation of this
method, even if you aren't using a NIB. (As a convenience, the default init method will do this for you,
and specify nil for both of this methods arguments.) In the specified NIB, the File's Owner proxy should
have its class set to your view controller subclass, with the view outlet connected to the main view. If you
invoke this method with a nil nib name, then this class' -loadView method will attempt to load a NIB whose
name is the same as your view controller's class. If no such NIB in fact exists then you must either call
-setView: before -view is invoked, or override the -loadView method to set up your views programatically.
*/
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
答案 1 :(得分:1)
当你覆盖init
方法时,它基本上意味着超类视图控制器访问器方法的属性,它具有navigationController设置为nil的属性,并且你实例化了tableView
属性代替。
您可以在viewDidLoad