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

  • <small id='GgAcS'></small><noframes id='GgAcS'>

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

    1. <tfoot id='GgAcS'></tfoot>

        • <bdo id='GgAcS'></bdo><ul id='GgAcS'></ul>

        如何在 Swift 中为 UIViewController 子类制作自定义初始化程序?

        How do I make a custom initializer for a UIViewController subclass in Swift?(如何在 Swift 中为 UIViewController 子类制作自定义初始化程序?)

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

          <tbody id='W14Kr'></tbody>
        <legend id='W14Kr'><style id='W14Kr'><dir id='W14Kr'><q id='W14Kr'></q></dir></style></legend>

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

                <tfoot id='W14Kr'></tfoot>
                  <bdo id='W14Kr'></bdo><ul id='W14Kr'></ul>
                  本文介绍了如何在 Swift 中为 UIViewController 子类制作自定义初始化程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  抱歉,如果之前有人问过这个问题,我已经搜索了很多,很多答案来自早期的 Swift 测试版,当时情况有所不同.我似乎找不到明确的答案.

                  Apologies if this has been asked before, I've searched around a lot and many answers are from earlier Swift betas when things were different. I can't seem to find a definitive answer.

                  我想继承 UIViewController 并有一个自定义初始化程序,以便我可以轻松地在代码中设置它.我在 Swift 中遇到了麻烦.

                  I want to subclass UIViewController and have a custom initializer to allow me to set it up in code easily. I'm having trouble doing this in Swift.

                  我想要一个 init() 函数,我可以使用它来传递特定的 NSURL,然后我将与视图控制器一起使用.在我看来,它类似于 init(withImageURL: NSURL).如果我添加该函数,它会要求我添加 init(coder: NSCoder) 函数.

                  I want an init() function that I can use to pass a specific NSURL I'll then use with the view controller. In my mind it looks something like init(withImageURL: NSURL). If I add that function it then asks me to add the init(coder: NSCoder) function.

                  我相信这是因为它在超类中使用 required 关键字进行了标记?所以我必须在子类中做到这一点?我添加它:

                  I believe this is because it's marked in the superclass with the required keyword? So I have to do it in the subclass? I add it:

                  required init(coder aDecoder: NSCoder) {
                      super.init(coder: aDecoder)
                  }
                  

                  现在呢?我的特殊初始化程序被认为是 convenience 吗?指定的?我要调用超级初始化器吗?来自同一个类的初始化器?

                  Now what? Is my special initializer considered a convenience one? A designated one? Do I call a super initializer? An initializer from the same class?

                  如何将我的特殊初始化程序添加到 UIViewController 子类中?

                  How do I add my special initializer onto a UIViewController subclass?

                  推荐答案

                  class ViewController: UIViewController {
                          
                      var imageURL: NSURL?
                      
                      // this is a convenient way to create this view controller without a imageURL
                      convenience init() {
                          self.init(imageURL: nil)
                      }
                      
                      init(imageURL: NSURL?) {
                          self.imageURL = imageURL
                          super.init(nibName: nil, bundle: nil)
                      }
                      
                      // if this view controller is loaded from a storyboard, imageURL will be nil
                      
                      required init?(coder aDecoder: NSCoder) {
                          super.init(coder: aDecoder)
                      }
                  }
                  

                  这篇关于如何在 Swift 中为 UIViewController 子类制作自定义初始化程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  How to format localised strings in Swift?(如何在 Swift 中格式化本地化字符串?)
                  Why do we need to set delegate to self? Why isn#39;t it defaulted by the compiler?(为什么我们需要将委托设置为自我?为什么编译器不默认它?)
                  set delegate and datasource of uitableview programmatically in Swift(在 Swift 中以编程方式设置 uitableview 的委托和数据源)
                  Swift delegate beetween two VC without segue(两个没有 segue 的 VC 之间的 Swift 委托)
                  Swift can#39;t call protocol method via delegate(Swift 不能通过委托调用协议方法)
                  <i id='Y9DMc'><tr id='Y9DMc'><dt id='Y9DMc'><q id='Y9DMc'><span id='Y9DMc'><b id='Y9DMc'><form id='Y9DMc'><ins id='Y9DMc'></ins><ul id='Y9DMc'></ul><sub id='Y9DMc'></sub></form><legend id='Y9DMc'></legend><bdo id='Y9DMc'><pre id='Y9DMc'><center id='Y9DMc'></center></pre></bdo></b><th id='Y9DMc'></th></span></q></dt></tr></i><div id='Y9DMc'><tfoot id='Y9DMc'></tfoot><dl id='Y9DMc'><fieldset id='Y9DMc'></fieldset></dl></div>
                    <bdo id='Y9DMc'></bdo><ul id='Y9DMc'></ul>

                          <tbody id='Y9DMc'></tbody>

                        • <legend id='Y9DMc'><style id='Y9DMc'><dir id='Y9DMc'><q id='Y9DMc'></q></dir></style></legend>

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

                          <tfoot id='Y9DMc'></tfoot>