ios - 当 IBOutlets 为零时,将自定义 View 添加到 Storyboard会崩溃

标签 ios storyboard swift xib

我有一个 View Controller ,它被内置于 Storyboard 中的 View 引用,在 Storyboard中我添加了一个 UIView 并将其类设置为我的新自定义 预览 View

PreviewView 内置于 xib 中,具有两个 UILabels 和一个 UIImageView。这些 subview 连接到 PreviewView 类中的 IBOutlets

预览 View 看起来像这样

class PreviewView: UIView {    

    @IBOutlet strong var imageView: UIImageView!
    @IBOutlet strong var addressLabel: UILabel!
    @IBOutlet strong var priceLabel: UILabel!

    func populate(address: String, price: Int, imageUrl:NSURL){
        self.addressLabel.text = address
        self.priceLabel.text = "\(price) kr"

        self.imageView.setImageWithURL(imageUrl)
    }
}

当在 view controller 中点击按钮调用 populate 时,应用程序在 self.addressLabel.text = address 作为 崩溃self.addressLabelnil

在 objective-c 中尝试过类似的代码,但没有出现相同的问题。这是 Swift 的问题还是我遗漏了什么?

最佳答案

它实际上是继承问题,当在子类中使用下面的 socket 时

 @IBOutlet strong var imageView: UIImageView!
 @IBOutlet strong var addressLabel: UILabel!
 @IBOutlet strong var priceLabel: UILabel!

这些 socket 也应该连接到子类。

关于ios - 当 IBOutlets 为零时,将自定义 View 添加到 Storyboard会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24887619/

相关文章:

ios - 如何在第二个 View Controller 中初始化 View Controller socket ?

ios - 无法将 kotlin 接口(interface)的快速实现传递给 kotlin native

ios - 如何在 Swift 中以编程方式更改 UIButton 状态

ios - 使用 Storyboard 将 NSString 传递到另一个 View

ios - 具有嵌套表格 View 的静态表格单元格的动态高度

ios - presentViewController 从 TabBarController 到外面

ios - 如何使整个区域可点击?

快速测量等级·角度转换精度

iphone - 同一单例类的方法名称部分?

ios - Swift 中的核心数据 : Only saving last object in a for loop