问题描述
我遇到了为UIViewController的子类编写自定义init的问题,基本上我想通过viewController的init方法传递依赖,而不是直接设置属性,如 viewControllerB.property = value
I'm having issue for writing custom init for subclass of UIViewController, basically I want to pass the dependency through the init method for viewController rather than setting property directly like viewControllerB.property = value
所以我为我的viewController做了一个自定义初始化,并调用超级指定init
So I made a custom init for my viewController and call super designated init
init(meme: Meme?) {
self.meme = meme
super.init(nibName: nil, bundle: nil)
}
视图控制器界面位于故事板中,我还将自定义类的界面作为我的视图控制器。即使你没有在这个方法中做任何事情,Swift也需要调用这个init方法。否则编译器会抱怨......
The view controller interface resides in storyboard, I've also make the interface for custom class to be my view controller. And Swift requires to call this init method even if you are not doing anything within this method. Otherwise the compiler will complain...
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
问题是当我尝试调用我的自定义init时 MyViewController(meme:meme)
它根本不在我的viewController中初始化属性...
The problem is when I try to call my custom init with MyViewController(meme: meme)
it doesn't init properties in my viewController at all...
我是试图调试,我发现在我的viewController中, init(编码器aDecoder:NSCoder)
首先调用,然后调用我的自定义init。但是这两个init方法返回不同的 self
内存地址。
I was trying to debug, I found in my viewController, init(coder aDecoder: NSCoder)
get called first, then my custom init get called later. However these two init method return different self
memory addresses.
我怀疑我的init有问题viewController,它将始终返回 self
与 init?(编码器aDecoder:NSCoder)
,没有实现。
I'm suspecting something wrong with the init for my viewController, and it will always return self
with the init?(coder aDecoder: NSCoder)
, which, has no implementation.
有谁知道如何正确地为viewController制作自定义初始化?
注意:我的viewController接口在storyboard中设置
Does anyone know how to make custom init for your viewController correctly ? Note: my viewController's interface is set up in storyboard
这是我的viewController代码:
here is my viewController code:
class MemeDetailVC : UIViewController {
var meme : Meme!
@IBOutlet weak var editedImage: UIImageView!
// TODO: incorrect init
init(meme: Meme?) {
self.meme = meme
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
/// setup nav title
title = "Detail Meme"
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
editedImage = UIImageView(image: meme.editedImage)
}
}
推荐答案
正如上面的一个答案中指出的那样,你不能使用和自定义init方法和故事板。
但您仍然可以使用静态方法从故事板中实例化ViewController并对其执行其他设置。
看起来像这样:
As it was specified in one of the answers above you can not use both and custom init method and storyboard.But you still can use a static method to instantiate ViewController from a storyboard and perform additional setup on it.It will look like this:
class MemeDetailVC : UIViewController {
var meme : Meme!
static func makeMemeDetailVC(meme: Meme) -> MemeDetailVC {
let newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("IdentifierOfYouViewController") as! MemeDetailVC
newViewController.meme = meme
return newViewController
}
}
不要忘记在Storyboard中指定IdentifierOfYouViewController作为视图控制器标识符。
您可能还需要在上面的代码中更改故事板的名称。
Don't forgot to specify IdentifierOfYouViewController as view controller identifier in your storyboard.You may also need to change name of storyboard in the code above.
这篇关于Swift中UIViewController的自定义初始化,在storyboard中设置接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!