Ios 无法创建可恢复视图的实例

Ios 无法创建可恢复视图的实例,ios,swift,xcode,Ios,Swift,Xcode,我正在使用以下代码: import UIKit class SnackBarView: UIView { @IBOutlet var containerView: UIView! @IBOutlet var labelText: UILabel! required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) let _ = commonInitiali

我正在使用以下代码:

import UIKit

class SnackBarView: UIView {

    @IBOutlet var containerView: UIView!

    @IBOutlet var labelText: UILabel!


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

        let _ = commonInitialization()

    }


    func commonInitialization() -> UIView
    {
        let bundle = Bundle.init(for: type(of: self))
        let nib = UINib(nibName: "SnackBarView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        addSubview(view)
        return view

    }

}
当我尝试这样初始化它时:

let snackBar = SnackBarView()
它给出了错误:

Missing argument for parameter 'coder' in call

我知道init函数中有错误,但我不确定如何更正。如何继续更正此问题?

您需要在视图中添加
覆盖init(frame:CGRect)
:更新的代码如下:

class SnackBarView: UIView {

    @IBOutlet var containerView: UIView!

    @IBOutlet var labelText: UILabel!


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

        let _ = commonInitialization()

    }

    // you need to add this init
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    func commonInitialization() -> UIView
    {
        let bundle = Bundle.init(for: type(of: self))
        let nib = UINib(nibName: "SnackBarView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        addSubview(view)
        return view

    }

}

您需要在视图中添加
override init(frame:CGRect)
:更新的代码如下:

class SnackBarView: UIView {

    @IBOutlet var containerView: UIView!

    @IBOutlet var labelText: UILabel!


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

        let _ = commonInitialization()

    }

    // you need to add this init
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    func commonInitialization() -> UIView
    {
        let bundle = Bundle.init(for: type(of: self))
        let nib = UINib(nibName: "SnackBarView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        addSubview(view)
        return view

    }

}

我认为用xib创建一个
UIView
,然后在其中添加一个与
commonInitialization
相同类型的实例是没有意义的,因为这里占据了所有的框架,我认为您需要

class SnackBarView: UIView {

    @IBOutlet var containerView: UIView!

    @IBOutlet var labelText: UILabel!

    class  func commonInitialization(_ rec:CGRect) -> SnackBarView
    {

        let nib = UINib(nibName: "SnackBarView", bundle: nil)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! SnackBarView
        view.frame = rec
        return view

    }

}
//


我认为用xib创建一个
UIView
,然后在其中添加一个与
commonInitialization
相同类型的实例是没有意义的,因为这里占据了所有的框架,我认为您需要

class SnackBarView: UIView {

    @IBOutlet var containerView: UIView!

    @IBOutlet var labelText: UILabel!

    class  func commonInitialization(_ rec:CGRect) -> SnackBarView
    {

        let nib = UINib(nibName: "SnackBarView", bundle: nil)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! SnackBarView
        view.frame = rec
        return view

    }

}
//


错误不在您的类中,而在您试图调用它的方式中

由于要创建具有零(0)个参数的
UIView
,因此需要为其提供一个构造函数(
init
),以便在发生这种情况时对其进行处理

希望下面的内容能够为您提供一个零(0)个参数的构造函数

class SnackBarView: UIView {

    @IBOutlet var containerView: UIView!

    @IBOutlet var labelText: UILabel!


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

        let _ = commonInitialization()
    }

    // you need to add this init
    override init(frame: CGRect) {
        super.init(frame: frame)

        // call your custom initialization code here
        let _ = commonInitialization()
    }

    // constructor with zero (0) parameters
    convenience init() {
        // call the override from above with a default CGRect
        self.init(frame: CGRect.zero)
    }

    func commonInitialization() -> UIView
    {
        let bundle = Bundle.init(for: type(of: self))
        let nib = UINib(nibName: "SnackBarView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        addSubview(view)
        return view
    }
}

错误不在您的类中,而在您试图调用它的方式中

由于要创建具有零(0)个参数的
UIView
,因此需要为其提供一个构造函数(
init
),以便在发生这种情况时对其进行处理

希望下面的内容能够为您提供一个零(0)个参数的构造函数

class SnackBarView: UIView {

    @IBOutlet var containerView: UIView!

    @IBOutlet var labelText: UILabel!


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

        let _ = commonInitialization()
    }

    // you need to add this init
    override init(frame: CGRect) {
        super.init(frame: frame)

        // call your custom initialization code here
        let _ = commonInitialization()
    }

    // constructor with zero (0) parameters
    convenience init() {
        // call the override from above with a default CGRect
        self.init(frame: CGRect.zero)
    }

    func commonInitialization() -> UIView
    {
        let bundle = Bundle.init(for: type(of: self))
        let nib = UINib(nibName: "SnackBarView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        addSubview(view)
        return view
    }
}

这根本没有实例化视图。这是为了解决编译时出现的错误。如果视图不出现,我将调试从XIB加载的代码。您是否尝试过在没有XIB的代码中创建
UIView
?这根本不会实例化视图。这是为了解决编译时出现的错误。如果视图不出现,我将调试从XIB加载的代码。您是否尝试过在没有XIB的代码中创建
UIView