<legend id='IkSmf'><style id='IkSmf'><dir id='IkSmf'><q id='IkSmf'></q></dir></style></legend>

<small id='IkSmf'></small><noframes id='IkSmf'>

  • <tfoot id='IkSmf'></tfoot>
      • <bdo id='IkSmf'></bdo><ul id='IkSmf'></ul>

    1. <i id='IkSmf'><tr id='IkSmf'><dt id='IkSmf'><q id='IkSmf'><span id='IkSmf'><b id='IkSmf'><form id='IkSmf'><ins id='IkSmf'></ins><ul id='IkSmf'></ul><sub id='IkSmf'></sub></form><legend id='IkSmf'></legend><bdo id='IkSmf'><pre id='IkSmf'><center id='IkSmf'></center></pre></bdo></b><th id='IkSmf'></th></span></q></dt></tr></i><div id='IkSmf'><tfoot id='IkSmf'></tfoot><dl id='IkSmf'><fieldset id='IkSmf'></fieldset></dl></div>

        何时应该使用 initWithNibName 初始化视图控制器?

        When should I initialize a view controller using initWithNibName?(何时应该使用 initWithNibName 初始化视图控制器?)

          <tfoot id='TtA9D'></tfoot>

            • <bdo id='TtA9D'></bdo><ul id='TtA9D'></ul>
            • <i id='TtA9D'><tr id='TtA9D'><dt id='TtA9D'><q id='TtA9D'><span id='TtA9D'><b id='TtA9D'><form id='TtA9D'><ins id='TtA9D'></ins><ul id='TtA9D'></ul><sub id='TtA9D'></sub></form><legend id='TtA9D'></legend><bdo id='TtA9D'><pre id='TtA9D'><center id='TtA9D'></center></pre></bdo></b><th id='TtA9D'></th></span></q></dt></tr></i><div id='TtA9D'><tfoot id='TtA9D'></tfoot><dl id='TtA9D'><fieldset id='TtA9D'></fieldset></dl></div>

              <small id='TtA9D'></small><noframes id='TtA9D'>

                    <tbody id='TtA9D'></tbody>
                • <legend id='TtA9D'><style id='TtA9D'><dir id='TtA9D'><q id='TtA9D'></q></dir></style></legend>
                • 本文介绍了何时应该使用 initWithNibName 初始化视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在创建视图控制器时,我应该什么时候使用 init:,什么时候应该使用 initWithNibName:bundle:?

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

                  推荐答案

                  -initWithNibName:bundle: 是 UIViewController 的指定初始化器.最终应该叫它.也就是说,尽管 Apple 的示例(在许多情况下倾向于简洁而不是可维护性),但绝不应该从视图控制器本身之外调用它.

                  -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.

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

                  You will often see code like this:

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

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

                  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:

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

                  然后,在 MYViewController 中:

                  Then, in 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;
                  }
                  

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

                  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.

                  这篇关于何时应该使用 initWithNibName 初始化视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  quot;Auto Layout still required after executing -layoutSubviewsquot; with UITableViewCell subclass(“执行 -layoutSubviews 后仍需要自动布局带有 UITableViewCell 子类)
                  How do I animate constraint changes?(如何为约束更改设置动画?)
                  iPhone to RS-232 via Bluetooth(iPhone 通过蓝牙转 RS-232)
                  How to change the preferred language of an iPhone app in iOS 4(如何在 iOS 4 中更改 iPhone 应用程序的首选语言)
                  iPhone app localization by releasing separate apps for different markets(通过为不同市场发布单独的应用程序来本地化 iPhone 应用程序)
                  Convert localizable.strings to spreadsheet?(将 localizable.strings 转换为电子表格?)

                      <tfoot id='oIlQm'></tfoot>
                      <i id='oIlQm'><tr id='oIlQm'><dt id='oIlQm'><q id='oIlQm'><span id='oIlQm'><b id='oIlQm'><form id='oIlQm'><ins id='oIlQm'></ins><ul id='oIlQm'></ul><sub id='oIlQm'></sub></form><legend id='oIlQm'></legend><bdo id='oIlQm'><pre id='oIlQm'><center id='oIlQm'></center></pre></bdo></b><th id='oIlQm'></th></span></q></dt></tr></i><div id='oIlQm'><tfoot id='oIlQm'></tfoot><dl id='oIlQm'><fieldset id='oIlQm'></fieldset></dl></div>

                      <small id='oIlQm'></small><noframes id='oIlQm'>

                        <bdo id='oIlQm'></bdo><ul id='oIlQm'></ul>
                      • <legend id='oIlQm'><style id='oIlQm'><dir id='oIlQm'><q id='oIlQm'></q></dir></style></legend>
                          <tbody id='oIlQm'></tbody>