awakeFromNib 中的变量为 nil

Variables is nil in awakeFromNib

我创建了一个自定义 UIView,我用对象 ConnectDetailItem 对其进行了实例化。

我的自定义视图代码:

class InfosConnectView: UIView {

    var view: UIView!

    @IBOutlet weak var categorie: UILabel!
    @IBOutlet weak var distance: UILabel!
    @IBOutlet weak var followers: UILabel!
    @IBOutlet weak var descriptionTextView: UITextView!

    var connectDetailsItem:ConnectDetailsItem!

    convenience init(connectDetailsItem:ConnectDetailsItem, frame:CGRect) {
        self.init(frame: frame)
        self.connectDetailsItem = connectDetailsItem
        xibSetup()
    }

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

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

    func xibSetup() {
        view = loadViewFromNib()

        // use bounds not frame or it'll be offset
        view.frame = bounds

        // Make the view stretch with containing view
        view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]

        view.layer.borderWidth = 1
        view.layer.borderColor = UIColor(hex: "#DDDDDD").cgColor

        // Adding custom subview on top of our view (over any custom drawing > see note below)
        addSubview(view)
    }

    func loadViewFromNib() -> UIView {

        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "viewInfosConnect", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

        return view
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        //Infos Connect
        self.categorie.text = "\(self.connectDetailsItem.category)"
        self.categorie.text = "\(self.connectDetailsItem.category)"
        if (self.connectDetailsItem.distance < 1000) {
            self.distance.text = "\(self.connectDetailsItem.distance) m"
        } else {
            let distance:NSString = NSString(format: "%.01f", Float(self.connectDetailsItem.distance)/1000)
            self.distance.text = "\(distance) km"
        }
        if(self.connectDetailsItem.followCount < 2) {
            if(self.connectDetailsItem.followCount < 1) {
                self.followers.text = "0 abonné"
            } else {
                self.followers.text = "\(self.connectDetailsItem.followCount) abonné"
            }
        } else {
            self.followers.text = "\(self.connectDetailsItem.followCount) abonnés"
        }
        self.descriptionTextView.text = self.connectDetailsItem.description
    }

}

awakeFromNib()中,connectDetailItemnil。为什么?

我这样实例化我的观点:let viewInfos = InfosConnectView(connectDetailsItem: self.connectDetailsItem, frame: CGRect(x: 0, y: 9, width: self.view.frame.width, height: 200))

我设置了断点并且在进入awakeFromNib函数之前connectDetailItem不为nil。

我找到了解决办法。 我将代码放在函数 loadViewFromNib 中,如下所示:

func loadViewFromNib() -> InfosConnectView {

        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "viewInfosConnect", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! InfosConnectView

        //Infos Connect
        view.categorie.text = "\(self.connectDetailsItem.category)"
        if (self.connectDetailsItem.distance < 1000) {
            view.distance.text = "\(self.connectDetailsItem.distance) m"
        } else {
            let distance:NSString = NSString(format: "%.01f", Float(self.connectDetailsItem.distance)/1000)
            view.distance.text = "\(distance) km"
        }
        if(self.connectDetailsItem.followCount < 2) {
            if(self.connectDetailsItem.followCount < 1) {
                view.followers.text = "0 abonné"
            } else {
                view.followers.text = "\(self.connectDetailsItem.followCount) abonné"
            }
        } else {
            view.followers.text = "\(self.connectDetailsItem.followCount) abonnés"
        }
        view.descriptionTextView.text = self.connectDetailsItem.description

        return view
    }