objective-c 什么时候应该使用 initWithNibName 初始化视图控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2224077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:54:10  来源:igfitidea点击:

When should I initialize a view controller using initWithNibName?

iphoneobjective-cuiviewcontrollerinitialization

提问by Anthony Glyadchenko

When should I use init:and when should I use initWithNibName:bundle:when creating a view controller?

创建视图控制器init:时应该initWithNibName:bundle:何时使用以及何时使用?

回答by Rob Napier

-initWithNibName:bundle:is the designated initializer for UIViewController. Something should eventually call it. That said, and despite Apple's examples (which favor brevity over maintainability in many cases), it should never be called from outside the view controller itself.

-initWithNibName:bundle:是 UIViewController 的指定初始值设定项。有些东西最终应该叫它。也就是说,尽管 Apple 的示例(在许多情况下倾向于简洁而不是可维护性),但永远不应从视图控制器本身外部调用它。

You will often see code like this:

你会经常看到这样的代码:

MYViewController *vc = [[MYViewController alloc] initWithNibName:@"Myview" bundle:nil];

I say this is incorrect. It puts implementation details (the name of the NIB and the fact that a NIB is even used) into the caller. That breaks encapsulation. The correct way to do this is:

我说这是不正确的。它将实现细节(NIB 的名称以及甚至使用 NIB 的事实)放入调用者中。这打破了封装。正确的做法是:

MYViewController *vc = [[MYViewController alloc] init];

Then, in MYViewController:

然后,在 MYViewController 中:

- (instancetype)init
{
   self = [super initWithNibName:@"Myview" bundle:nil];
   if (self != nil)
   {
       // Further initialization if needed
   }
   return self;
}

- (instancetype)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
    NSAssert(NO, @"Initialize with -init");
    return nil;
}

This moves the key implementation details back into the object, and prevents callers from accidentally breaking encapsulation. Now if you change the name of the NIB, or move to programmatic construction, you fix it in one place (in the view controller) rather than in every place the view controller is used.

这将关键实现细节移回对象,并防止调用者意外破坏封装。现在,如果您更改 NIB 的名称,或转向程序化构造,则将其固定在一个位置(在视图控制器中),而不是在使用视图控制器的每个位置。

回答by bpapa

Use initWithNibName: bundle:if you are... initializing with a nib file! That is, a file that you made using Interface Builder.

initWithNibName: bundle:如果您正在...使用 nib 文件初始化,请使用!也就是说,您使用 Interface Builder 创建的文件。

If you aren't using IB to layout your views, you can just use init.

如果你不使用 IB 来布局你的视图,你可以使用init.

回答by Kiet Nguyen

You can just call init, as long as the xib has the same name as the view controller class. The encapsulation is not necessary. This saves typing, but may not serve clarity.

只要 xib 与视图控制器类具有相同的名称,您就可以调用 init。封装不是必需的。这可以节省打字的时间,但可能无法提供清晰度。

 NUDMainViewController *mainVC = [[NUDMainViewController alloc] init];

回答by JosephT

using init when there is no nib/xib file, e.g. UI are created by coding

在没有 nib/xib 文件时使用 init,例如 UI 是通过编码创建的

using initWithNibName , if we have an nib/xib or same controller share by more than 1 nib/xib

使用 initWithNibName ,如果我们有超过 1 个 nib/xib 的 nib/xib 或相同的控制器共享

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
 } else {
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
 }

that's what I think..

那就是我所想的..