我通过xib实例化一个商店:
let cShop = UINib(nibName: "connectedShop", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! connectedShop
这家商店已经分配了这个定制类:
import Foundation import UIKit class connectedShop : UIView{ @IBOutlet weak var bannerViewHight: NSLayoutConstraint! @IBOutlet weak var bannerViewTop: NSLayoutConstraint! @IBOutlet weak var bannerViewLeft: NSLayoutConstraint! @IBOutlet weak var bannerViewRight: NSLayoutConstraint! @IBOutlet weak var banerDiscount: UIImageView! @IBOutlet weak var bannerImageShop: UIImageView! @IBOutlet weak var bannerTitle: UILabel! @IBOutlet weak var bannerDescription: UILabel! @IBOutlet weak var bannerButton: UIButton! @IBAction func bannerButtonAction(sender: UIButton) { } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! } }
从代码中我称这个店为:
let cShop = UINib(nibName: "connectedShop", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! connectedShop cShop.bannerTitle.text = "shopTitle" //work shopContainer.addSubview(cShop) cShop.bannerViewLeft.constant = 0 //make my app crash cShop.bannerViewRight.constant = 0 // " cShop.bannerViewTop.constant = 0 // "
当我设置bannerTitle.text
属性我的应用程序继续和调试我看到这个出口已初始化,但约束不是,当编译器在分配常量的行中时,它总是抛出相同的错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
调试我看到有一个初始化的插座:
有人知道为什么会发生这种情况吗?
这些是我的标题:
我遇到过同样的问题.将约束出口更改为strong.
从声明中删除弱.
@IBOutlet weak var bannerViewHight: NSLayoutConstraint!
至
@IBOutlet var bannerViewHight: NSLayoutConstraint!
为所有人做这件事就是这样.