情节提要中的UIViewController的自定义初始化 [英] Custom init of UIViewController from storyboard

查看:146
本文介绍了情节提要中的UIViewController的自定义初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一些相关问题,但什么也没得到,希望有人能帮忙.

I tried finding some relevant questions but couldn't get anything, hope someone can help.

我在情节提要板上设置了一些UIViewController.然后,我想在代码中加载视图控制器之一,并将其​​推入导航堆栈.我发现正确的方法是使用

I set up some UIViewController's on a storyboard. I then want to load one of the view controllers in code and push it onto the navigation stack. I figure out the right way to do this is to use

instantiateViewControllerWithIdentifier

这会调用init(coder: NSCoder),一切都很好,我的程序可以运行,但是我希望能够有一个自定义的初始化程序,该初始化程序为我的视图控制器设置一些变量.请考虑以下内容:

This calls init(coder: NSCoder) and all is well, my program works, but I want to be able to have a custom initializer that sets up some variables for my view controller. Consider the following:

class A : UIViewController {   

  let i : Int

  required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    // property self.i not initialized at super.init call   
  }

}

由于在创建对象时需要指定i,因此我显然会出错.有什么解决办法吗?我对将i声明为var并在以后对其进行配置不感兴趣,因为这不符合要求,并且我不再具有编译器保证i是不可变的.

I obviously get an error since i needs to be specified at time of object creation. Any solutions to this? I am not interested in declaring i as var and configuring it later as that defeats the point and I no longer have a compiler guarantee that i is immutable.

假设我当前加载的ViewController具有一些变量i.此值是可变的,可以更改.现在,假设我要从这个ViewController中展示另一个,并使用i对其进行初始化.

Suppose I have a currently loaded ViewController that has some variable i. This value is variable and can change. Now suppose from this ViewController I want to present another one, and initialize it with i.

class ViewController: UIViewController {

   var i : Int

   // ... other things

  // in response to some button tap...
  @IBAction func tappedButton(sender: AnyObject) {
    let st = UIStoryboard(name: "Main", bundle: nil)
    let vc = st.instantiateViewControllerWithIdentifier("AControllerID") as! A
    // How do I initialize A with i ?
    self.presentViewController(vc, animated: true, completion: nil)
  }

}

我似乎无法执行此操作,并且使用let而不是var来保持i不可变.

I don't seem to be able to do this and keep i immutable by using let instead of var.

推荐答案

我先前的答案的一种简化方法,该方法既快速又避免了其他hacky修复程序:

A simplification of my prior answer which is quick and avoids alternative hacky fixes:

这里是您可能要从设置了objectID的情节提要中实例化的详细视图控制器:

Here is a detail view controller you may want to instantiate from storyboard with an objectID set:

import UIKit

class DetailViewController: UIViewController {

    var objectID : Int!

    internal static func instantiate(with objectID: Int) -> DetailViewController {

        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailViewController") as DetailViewController
        vc.objectID = objectID
        return vc
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if((objectID) != nil){
            print("Here is my objectID: \(objectID)")
        }
    }
}

在这里,您将使用它来将其推送到objectID设置为1的导航控制器上.

Here is how you would use it to push onto a navigation controller with objectID set to 1:

self.navigationController.pushViewController(DetailViewController.instantiate(1), animated: true)

添加了博客文章: https://theswiftcook.wordpress.com/2017/02/17/how-to-initialize-a-storyboard-viewcontroller-with-data-without-segues-swift-3-0git/

链接到GitHub上的示例: https://github.com/hammadzz/Instantiate-ViewController-From-Storyboard -随数据

这篇关于情节提要中的UIViewController的自定义初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆