2

我创建了一个使用对象 ConnectDetailItem 实例化的自定义 UIView。

我的自定义视图代码:

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 不为零。

4

1 回答 1

1

我找到了解决方案。我将代码放在函数 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
    }
于 2016-11-09T15:25:57.700 回答