2011-05-17 85 views
0

如何覆盖UINavigationController中的initWithRootViewController方法?如何覆盖initWithRootViewController

xcode对我来说唯一的方法是在loadFromNibName和loadView等方法。这些方法不会被调用,我需要在启动时将NSNotification添加到navigationcontroller。

我知道这看起来有点像下面的,但我不知道放什么方法体

- (id)initWithRootViewController:(UIViewController *)rootViewController 

{ 
// what goes here? 
} 

编辑 我想真正的问题是“你怎么自定义初始化”

编辑2

我的导航控制器头

期间的UIViewController
@interface AccountViewNavigationController : UINavigationController { 

} 
@end 

实例化我的UINavigationController像这样将导致没有启动方法打破发点

accountViewNavController = [[UINavigationController alloc] initWithRootViewController:accountView]; 

在哪里,如果我实例,像这样的loadView不会被调用....但它被调用无数次

accountViewNavController = [[UINavigationController alloc] init]; 

[accountViewNavController initWithRootViewController:accountView NO]; 

我对这个阶段非常困惑。

+0

你为什么要重写它?如果你正在使用xib,你可以简单地将RootViewController类改为另一个UIViewController子类 – lostInTransit 2011-05-17 11:03:20

+0

我想添加以下内容.. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveMyEvent :) name:@“MyEvent”object :零]; – dubbeat 2011-05-17 11:05:00

回答

2

使用您用于覆盖任何其他init方法相同的基本结构:

- (id)initWithRootViewController:(UIViewController *)rootViewController 
{ 
    if ((self = [super initWithRootViewController:rootViewController])) { 
     // Your modifications go here 
    } 
    return self; 
} 

请注意,苹果公司声称的UINavigationController是“不适合子类化”,但他们没有绝对禁止它。我想这意味着“不要试图通过搞乱内部消息流来改变类的工作方式”。

+0

嗯...我卡在那里的一个断点,它似乎仍然没有进入功能? – dubbeat 2011-05-17 10:58:04

+0

@dubbeat:你确定你使用自己的子类而不是股票UINavigationController?你使用IB创建它,在这种情况下,你需要使用'awakeFromNib'或可能'initWithCoder:'?或者你可以在'viewDidLoad','viewWillAppear:'或类似的代码中执行你的代码吗? – Anomie 2011-05-17 12:49:03

+0

我不确定它是否是“股票”。我没有使用IB。在代码中做,如果我像这样实例化,viewDidLoad只会被调用。 accountViewNavController = [[UINavigationController alloc] init] ;. viewDidload被多次调用,这对我来说很奇怪? – dubbeat 2011-05-17 12:56:35