Objective c self.NavigationController为nil,即使使用initWithRootViewController调用也是如此

Objective c self.NavigationController为nil,即使使用initWithRootViewController调用也是如此,objective-c,uinavigationcontroller,Objective C,Uinavigationcontroller,我不知道我做错了什么,我有其他关于它的帖子,但它们似乎与一个更清晰的案例有关,在我的情况下,一切似乎都很简单,但仍然不起作用 在我的AppDelegate.m中 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { MyViewController *vc = [[MyViewController alloc] init]

我不知道我做错了什么,我有其他关于它的帖子,但它们似乎与一个更清晰的案例有关,在我的情况下,一切似乎都很简单,但仍然不起作用

在我的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文件)

其结果是:

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正确实例化,否则不会。有人能解释一下为什么会发生这种情况吗?

您的init方法中有什么?你重写了那个方法吗?如果不是,您可能应该或使用
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;

当您重写
init
方法时,它基本上意味着超级类视图控制器访问器方法的属性,该方法的属性navigationController设置为nil,而您将实例化
tableView
属性


您可以在
viewdiload

中设置tableview样式和其他属性。实际上,您已经非常接近了,也许您可以帮助我了解我刚刚找到的解决方案的原因。我现在要编辑我的问题。谢谢是的,将tableview样式移动到viewDidLoad是我现在所做的,但是您确定您的说法吗?当我编写时:
self=[super initWithStyle:style]我希望正确实例化我的超类,而不是nil-led。您的超类方法将被重写。导航属性根本没有设置。它是零的。另一方面,tableView属性方法已经设置好了,我也遇到了同样的问题,我仍然不相信答案是正确的,因为我没有重写任何init方法。如果我访问ViewWillDisplay中的navigationController属性,那么它实际上会正确地找到控制器。。。
/*
  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;