菜鸟教程小白 发表于 2022-12-12 19:03:03

ios - UIViewController 的 initWithNibName : a reason behind this design?


                                            <p><p>我倾向于同意 Joe Conway 和 Aaron Hillegass 的分析,正如 Ole Begemann 今天在 <a href="http://oleb.net/blog/2012/01/initWithNibName-bundle-breaks-encapsulation/" rel="noreferrer noopener nofollow">http://oleb.net/blog/2012/01/initWithNibName-bundle-breaks-encapsulation/</a> 中所报道的那样</p>

<p>基本上,他们声明 NIB 的文件名是相应 UIViewController 类的实现细节,在 init 方法中传入 NIB 的文件名不是调用类的业务。 </p>

<p>我想知道 AppKit/UIKit 的创建者选择这种设计是否有任何特殊原因,或者这仅仅是一个错误——在后一种情况下,为什么 UIKit 出现时没有纠正它,这将是一个很好的机会。 </p>

<p>如果有任何 Objective-C 的老前辈可以提供这方面的历史背景,那将有助于更好地了解我们每天使用的框架。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我怀疑这样做是为了使 <code>UIViewController</code> 可以具有作为 Controller 的基本功能,而无需任何子类化。例如,如果您只是在导航 Controller 上推送“Credits” View ,并且该 View 只有静态文本,那么您可以不创建 <code>UIViewController</code> 子类。您可以直接创建一个 <code>UIViewController</code> 并将包含静态文本的 nib 传递给它。</p>

<p>当然,大多数情况下,您都希望与呈现的内容进行某种程度的交互,在这种情况下,自定义 Controller 是必要的。但理论上,它并不总是必需的。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIViewController 的 initWithNibName : a reason behind this design?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8976873/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8976873/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIViewController 的 initWithNibName : a reason behind this design?