在Swift中调用参数'coder'时缺少参数

时间:2021-02-10 09:40:20
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.

一个视图控制器(SecondViewController),UIViewController固有的需要一个指定的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.

我理解必须提供编码器参数,但想知道它的价值来自何处。

2 个解决方案

#1


Thanks for the answers from @Chackle. Finally the solution I figured out is below.

感谢@Chackle的回答。最后,我想出的解决方案如下。

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. And so is "super.init(nibName: nil, bundle: nil)", because it is the way how UIViewController does initialization.

如果你创建了一个UIViewController的子类,那么必需“init(coder aDecoder:NSCoder)”。 “super.init(nibName:nil,bundle:nil)”也是如此,因为它是UIViewController初始化的方式。

#2


initWithCoder is a constructor specifically for the Interface Builder. This is the constructor that is called when you create a UIViewController in the interface builder.

initWithCoder是专门用于Interface Builder的构造函数。这是在界面构建器中创建UIViewController时调用的构造函数。

You instantiate a view controller via this by using 'segues'. If you do not already know about segues I would recommend checking up on some documentation / tutorials to do with them. It really helps explain things.

您可以通过使用'segues'来实例化视图控制器。如果您还不了解segues,我建议您查看一些文档/教程来处理它们。这真的有助于解释事情。

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/

这个小教程教你如何正确使用segue,甚至在View Controllers之间传递数据: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)

#1


Thanks for the answers from @Chackle. Finally the solution I figured out is below.

感谢@Chackle的回答。最后,我想出的解决方案如下。

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. And so is "super.init(nibName: nil, bundle: nil)", because it is the way how UIViewController does initialization.

如果你创建了一个UIViewController的子类,那么必需“init(coder aDecoder:NSCoder)”。 “super.init(nibName:nil,bundle:nil)”也是如此,因为它是UIViewController初始化的方式。

#2


initWithCoder is a constructor specifically for the Interface Builder. This is the constructor that is called when you create a UIViewController in the interface builder.

initWithCoder是专门用于Interface Builder的构造函数。这是在界面构建器中创建UIViewController时调用的构造函数。

You instantiate a view controller via this by using 'segues'. If you do not already know about segues I would recommend checking up on some documentation / tutorials to do with them. It really helps explain things.

您可以通过使用'segues'来实例化视图控制器。如果您还不了解segues,我建议您查看一些文档/教程来处理它们。这真的有助于解释事情。

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/

这个小教程教你如何正确使用segue,甚至在View Controllers之间传递数据: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)