为什么在awakeFromNib()中此View为nil? [英] Why is this View nil in awakeFromNib()?

查看:211
本文介绍了为什么在awakeFromNib()中此View为nil?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在练习awakeFromNib.

I'm practicing awakeFromNib.

我在Main.storyboard中只有一个UIView. 此UIView继承class CardView.

I have a single UIView in Main.storyboard. This UIView inherit class CardView.

代码在

class ViewController: UIViewController {

    @IBOutlet weak var cardView: CardView!

}

我有一个CardView.xib.在CardView.xib中,有一个默认的UIView继承了class CardView.在此UIView中,还有另一个继承了class CardContentView的单一视图. 当然,我有一个CardView.swift,其中有class CardView.

And I have a CardView.xib. In, CardView.xib, there's a default single UIView which inherits a class CardView. And inside this UIView, there's the other single view which inherits a class CardContentView. And Of course, I have a CardView.swift which have class CardView.

代码在下面

class CardView: UIView {

    @IBOutlet weak var cardContentView: CardContentView!

    override func awakeFromNib() {
        cardContentView.layer.cornerRadius = 16
    }
}

我有一个CardContentView.xib.在CardContentView.xib中,有一个默认的UIView,它继承了默认的class UIView. 当然,我有一个CardContentView.swift,其中有class CardContentView.

And I have a CardContentView.xib. In, CardContentView.xib, there's a default single UIView which inherits a default class UIView. And Of course, I have a CardContentView.swift which have class CardContentView.

代码在下面

class CardContentView: UIView {

    @IBOutlet var backgroundView: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

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

    private func commonInit(){
        Bundle.main.loadNibNamed("CardContentView", owner: self, options: nil)
        self.addSubview(backgroundView)
    }

}

是的,我想通过class CardView在ViewController中将CardContentView显示为cardView.

Yes, I want to show the CardContentView to cardView in ViewController through class CardView.

但是当我运行时,错误会弹出class CardView

But when I run, the error pops in class CardView

错误行在func awakeFromNib

具体行是cardContentView.layer.cornerRadius = 16.

错误消息是Unexpectedly found nil while unwrapping an Optional value.尤其是cardContentView = nil.

我不知道为什么cardContentView = nil.

我将Xib板中的cardContentView链接到CardView.swift中的class CardView.

I linked cardContentView in Xib board to class CardView in CardView.swift.

我应该如何修改代码以运行此代码?

And how should I modify the code to run this?

谢谢!

推荐答案

首先,您需要在主故事板上连接cardView插座,当然,您已经在ViewController中添加了一个视图.然后,在CardView类中,您必须实例化cardContentView出口,因为在CardView.xib中您没有对cardContentView的任何引用.确保已连接CardContentView.xib中的backgroundView插座.

First of all, you need to connect the cardView outlet in the main storyboard, and of course, you've added a view in your ViewController. Then in your CardView class, you have to instantiate cardContentView outlet, because in CardView.xib you do not have any reference to cardContentView. Make sure that you've connected backgroundView outlet in CardContentView.xib.

class CardView: UIView {

    @IBOutlet weak var cardContentView: CardContentView!

    override func awakeFromNib() {
        super.awakeFromNib()
        cardContentView = Bundle.main.loadNibNamed("CardContentView", owner: self, options: nil)!.first as! CardContentView
        self.addSubview(cardContentView)
        cardContentView.layer.cornerRadius = 16

        //you can also get access to the subviews of cardContentView
        //cardContentView.backgroundView.backgroundColor = .red
    }
}


class CardContentView: UIView {

    @IBOutlet var backgroundView: UIView!

    override func awakeFromNib() {
        backgroundView.backgroundColor = .yellow
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        //commonInit()
    } 

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

    private func commonInit() {
    }
}

这篇关于为什么在awakeFromNib()中此View为nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆