来自故事板的 UIViewController 的自定义初始化 [英] Custom init of UIViewController from storyboard
问题描述
我试着找了一些相关的问题,但没有得到任何东西,希望有人能帮忙.
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)")
}
}
}
以下是您将如何使用它来推送对象 ID 设置为 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)
链接到 GitHub 上的示例:https://github.com/hammadzz/Instantiate-ViewController-From-Storyboard-With-Data
这篇关于来自故事板的 UIViewController 的自定义初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!