Swift 中 UIViewController 的自定义初始化,并在情节提要中设置界面

Custom init for UIViewController in Swift with interface setup in storyboard(Swift 中 UIViewController 的自定义初始化,并在情节提要中设置界面)

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

问题描述

我在为 UIViewController 的子类编写自定义 init 时遇到问题,基本上我想通过 viewController 的 init 方法传递依赖关系,而不是像 viewControllerB.property = value

所以我为我的 viewController 做了一个自定义的 init 并调用了 super 指定的 init

init(meme: Meme?) {self.meme = memesuper.init(笔尖名称:无,捆绑:无)}

视图控制器接口位于情节提要中,我还将自定义类的接口作为我的视图控制器.即使你没有在这个方法中做任何事情,Swift 也需要调用这个 init 方法.否则编译器会报错...

需要初始化?(coder aDecoder: NSCoder) {super.init(编码器:aDecoder)}

问题是当我尝试使用 MyViewController(meme: meme) 调用我的自定义初始化时,它根本不会在我的 viewController 中初始化属性...

我正在尝试调试,我在我的 viewController 中发现,首先调用 init(coder aDecoder: NSCoder),然后再调用我的自定义 init.然而这两个 init 方法返回不同的 self 内存地址.

我怀疑我的 viewController 的 init 有问题,它总是会返回带有 init?(coder aDecoder: NSCoder)self,其中有没有实现.

有谁知道如何正确地为您的 viewController 进行自定义初始化?注意:我的viewController的界面是在storyboard里设置的

这是我的 viewController 代码:

类 MemeDetailVC : UIViewController {var meme : 模因!@IBOutlet 弱 var 已编辑图像:UIImageView!//TODO: 不正确的初始化初始化(模因:模因?){self.meme = memesuper.init(笔尖名称:无,捆绑:无)}需要初始化?(编码器 aDecoder:NSCoder){super.init(编码器:aDecoder)}覆盖 func viewDidLoad() {///设置导航标题标题=详细模因"super.viewDidLoad()}覆盖 func viewWillAppear(动画:布尔){super.viewWillAppear(动画)编辑图像 = UIImageView(图像:meme.editedImage)}}

解决方案

正如在上面的答案之一中指定的那样,您不能同时使用自定义 init 方法和情节提要.

但您仍然可以使用静态方法从情节提要中实例化 ViewController 并对其执行其他设置.

看起来像这样:

类 MemeDetailVC : UIViewController {var meme : 模因!静态函数 makeMemeDetailVC(meme: Meme) ->MemeDetailVC {让 newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "IdentifierOfYouViewController") 为!MemeDetailVCnewViewController.meme = meme返回新视图控制器}}

不要忘记在情节提要中将 IdentifierOfYouViewController 指定为视图控制器标识符.您可能还需要在上面的代码中更改情节提要的名称.

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

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)
    }

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)
    }

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...

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.

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.

Does anyone know how to make custom init for your viewController correctly ? Note: my viewController's interface is set up in storyboard

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)
    }

}

解决方案

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).instantiateViewController(withIdentifier: "IdentifierOfYouViewController") as! MemeDetailVC
        
        newViewController.meme = meme
        
        return newViewController
    }
}

Don't forget to specify IdentifierOfYouViewController as view controller identifier in your storyboard. You may also need to change the name of the storyboard in the code above.

这篇关于Swift 中 UIViewController 的自定义初始化,并在情节提要中设置界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Swift 中 UIViewController 的自定义初始化,并在情节提要中设置界面

基础教程推荐