简体   繁体   English

Swift中调用中缺少参数'coder'的参数

[英]Missing argument for parameter 'coder' in call in Swift

class SecondViewController:UIViewController {
  required init(coder aDecoder: NSCoder){
    super.init(coder: aDecoder)
    //Custom logic here
  }
}

Quite a newbie question: 相当新手的问题:

a view controller(SecondViewController), inherent from UIViewController needs a designated init function like above. UIViewController固有的一个视图控制器(SecondViewController)需要一个如上所述的指定init函数。

In this case, how should I call it, given I am not sure what the value for "coder" should be? 在这种情况下,如果我不确定“编码器”的值应该是什么,我应该怎么称呼它? I used to call the controller as: SecondViewController(), but it gives: 我以前将控制器称为:SecondViewController(),但它给出了:

Missing argument for parameter 'coder' in call

I understand coder parameter has to be provided, but want to ask what its value comes from. 我了解必须提供编码器参数,但想问问它的值来自哪里。

Thanks for the answers from @Chackle. 感谢@Chackle的回答。 Finally the solution I figured out is below. 最后,我想出的解决方案如下。

What I want: 我想要的是:

  • Inherit my SecondViewController from UIViewController UIViewController继承我的SecondViewController
  • Simply to initialize any new SecondViewController as SecondViewController() 只需将任何新的SecondViewController初始化为SecondViewController()

Solution: 解:

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

    init() {
        super.init(nibName: nil, bundle: nil)
        //Do whatever you want here
    }

"required init(coder aDecoder: NSCoder)" is a must if you create a subclass of UIViewController . 如果创建UIViewController的子类,则必须"required init(coder aDecoder: NSCoder)" And so is "super.init(nibName: nil, bundle: nil)" , because it is the way how UIViewController does initialization. "super.init(nibName: nil, bundle: nil)" ,因为这是UIViewController初始化的方式。

initWithCoder is a constructor specifically for the Interface Builder. initWithCoder是专门用于接口生成器的构造函数。 This is the constructor that is called when you create a UIViewController in the interface builder. 这是在接口构建器中创建UIViewController时调用的构造器。

You instantiate a view controller via this by using 'segues'. 您可以通过使用“ segues”实例化一个视图控制器。 If you do not already know about segues I would recommend checking up on some documentation / tutorials to do with them. 如果您还不了解segue,我建议您查阅一些文档/教程以了解有关它们的信息。 It really helps explain things. 它确实有助于解释事情。

This little tutorial teaches you how to use segues correctly and even pass data between View Controllers: http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/ 本小教程将教您如何正确使用Segues甚至在View Controller之间传递数据: http : //www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/

EDIT: If you wish to not use segues then perhaps this bit of code might be of some help to you (you still need to create the ViewController in the interface builder however) 编辑:如果您不希望使用segues,那么这部分代码可能会对您有所帮助(但是您仍然需要在界面生成器中创建ViewController)

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM