initWithNibName ....方法没有触发

时间:2012-02-08 10:05:08

标签: iphone objective-c ios ios4

在我的基于导航的应用程序中,initWithNibName方法未调用/ fire

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

我正在这样的appdelegate中定义rootViewController(calViewController)

calViewController *objCalViewController = (calViewController *) [navController topViewController];

objCalViewController.context = [self managedObjectContext];


[window addSubview:navController.view];
[window makeKeyAndVisible];

这是问题吗?请给我一个帮助

3 个答案:

答案 0 :(得分:10)

如果您在nib文件或Storyboard中定义了UIViewController(通常为IBOutlet),则不会调用initWithNibName:bundle:,而是initWithCoder:。当您使用Interface Builder将UIViewController设置为UITabBarControllerUINavigationController的一部分时,几乎总是在使用Storyboard时,就是这种情况。

答案 1 :(得分:1)

你的appdelegate.m中的

就是这样的

calViewController *objCalViewController = [[calViewController alloc] initWithNibName:@"WebViewController_iPhone" bundle:nil];
objCalViewController.managedObjectContext = self.managedObjectContext;
navController = [[UINavigationController alloc] initWithRootViewController:objCalViewController];
[self.window setRootViewController:navController];
[window makeKeyAndVisible];

答案 2 :(得分:0)

好吧,假设我有一个名为TCViewController

的viewController
TCViewController.h, TCViewController.m and TCViewController.xib

并在TCViewController.m 我推翻以下方法。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self) {
       // Custom initialization
   }
   return self;
}

在我的RootViewController中,我想像这样初始化TCViewController

RootViewController.m

-(void)viewDidLoad //not necessarily this one, can be any method
{
 // This initialization calls the initWithNibname method implemented in TCViewController.m
TCViewController *viewController  = [[TCViewController alloc] initWithNibName:@"TCViewController" bundle:nil];

[self.navigationController pushViewController:viewController animated:YES];


}

如果你像这样初始化一个viewController,控件将传递给子类中的initWithNibName方法,如果你已经实现了它。