【发布时间】:2014-09-13 06:54:25
【问题描述】:
我有一个视图控制器,它被 Storyboard 中内置的视图所引用,在 Storyboard 中我添加了一个 UIView 并将它的类设置为我的新自定义 PreviewView。
PreviewView 内置在 xib 中,有两个 UILabels 和一个 UIImageView。这些子视图连接到PreviewView 类中的IBOutlets。
PreviewView 看起来像这样
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.addressLabel 是nil
在目标 C 中尝试过类似的代码,但那里没有同样的问题。这是 Swift 问题还是我遗漏了什么?
【问题讨论】:
-
不要在 Storyboard 中添加 UIView 将其作为新文件添加到项目的 xib 文件中
-
在项目中添加为xib文件。在视图控制器中使用时,它仍会添加到情节提要中的实际视图中。
-
我不想在代码中实例化它,而是让情节提要从 xib 加载它,因为它在 swift 之前工作得很好。
标签: ios storyboard swift xib