initWithNibName ...方法未触发

发布于 2025-01-03 03:57:32 字数 631 浏览 1 评论 0原文

在我的基于导航的应用程序中,initWithNibName 方法不会被调用/触发

- (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];

这是问题所在吗?请给我帮助

In my navigation based application, initWithNibName method does not called/fire

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

I'm defining rootViewController (calViewController) in appdelegate like this

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

objCalViewController.context = [self managedObjectContext];


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

Is this is the issue? Please give me a help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一向肩并 2025-01-10 03:57:32

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

When your UIViewController is defined in a nib file or Storyboard (usually as an IBOutlet), initWithNibName:bundle: is not called, rather initWithCoder: is. This is the case when you use Interface Builder to set your UIViewController as part of UITabBarController or UINavigationController, and almost always when using Storyboards.

╭ゆ眷念 2025-01-10 03:57:32

在你的 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];

in your appdelegate.m do like this

calViewController *objCalViewController = [[calViewController alloc] initWithNibName:@"WebViewController_iPhone" bundle:nil];
objCalViewController.managedObjectContext = self.managedObjectContext;
navController = [[UINavigationController alloc] initWithRootViewController:objCalViewController];
[self.window setRootViewController:navController];
[window makeKeyAndVisible];
仅此而已 2025-01-10 03:57:32

好吧,假设我有一个名为 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 方法(如果你已经实现了它)。

Ok lets say i have a viewController named TCViewController with

TCViewController.h, TCViewController.m and TCViewController.xib

and in TCViewController.m
i am overriding the below method.

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

In my RootViewController i want to initialize TCViewController like this

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];


}

If you initialize a viewController like this, control will pass to the initWithNibName method in your subclass if you have implemented it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文