ios - NSLayoutConstraint outlets 始终为 nil 而其他 outlets 则不是

标签 ios swift swift2 storyboard interface-builder

我正在通过 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

调试我看到有一个 socket 被初始化:

enter image description here

有人知道为什么会这样吗?

这些是我的连接:

enter image description here

最佳答案

我遇到了同样的问题。 将您的约束导出更改为强。

关于ios - NSLayoutConstraint outlets 始终为 nil 而其他 outlets 则不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38475202/

相关文章:

ios - 如何支持使用 UILabel 自动调整 UITableViewCell 的大小

ios - 无法通过 UIView 通过 swift 创建 pdf?

ios - 无法访问 Firebase Snap 数据

ios - 有没有便捷的方法可以在 Pinterest IOS SDK 中删除 PIN 码?

objective-c - CLLocation 没有显示准​​确的结果

ios - 在 iOS Swift 中搜索多个子字符串的字符串

ios - stringByExpandingTildeinPath 不可用

objective-c - 应用程序中的 iOS 内部代理

iOS - 通用链接错误

ios - 计算视频文件md5(hash)