Xcode - IBOutlets 和 Nib 文件如何工作?
Xcode - How IBOutlets and Nib files work?
我是第一次使用 nib 文件。我的意思是xib和对应的swiftclass.
这是我的 swift class:
import UIKit
@IBDesignable class LittleVideoView: UIView {
var view: UIView!
var nibName: String = "LittleVideoView"
// MARK: Views
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var clicksLabel: UILabel!
@IBOutlet weak var channelNameLabel: UILabel!
@IBOutlet weak var thumbnailImageView: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: nibName, bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
}
如您所见,我在编辑器视图中创建了一些 IBOutlets。一切正常。
只是有些事情我不明白。我在这个 class 中以编程方式加载 nib,那么为什么我可以创建 IBOutlets 而 Xcode 并不知道我真的会加载正确的 nib 文件?很快,我不明白 IBOutlets 在这种情况下如何工作。那么,Xcode 如何使用 IBOutlets 更正 link setup() 方法中加载的 UIView 呢?
Nib 文件有一个 "File's Owner" 类型,编辑器使用它来列出可用的出口和操作。但是,当在运行时加载 nib 时,不会强制执行该类型。插座使用 -setValue:forKey:
连接,假设传递给 instantiateWithOwner
的 "Owner" 与 nib 中定义的任何插座绑定兼容。
One xib File with Multiple "File's Owner"s
If you have an IBOutlet, but not a property, is it retained or not?
我是第一次使用 nib 文件。我的意思是xib和对应的swiftclass.
这是我的 swift class:
import UIKit
@IBDesignable class LittleVideoView: UIView {
var view: UIView!
var nibName: String = "LittleVideoView"
// MARK: Views
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var clicksLabel: UILabel!
@IBOutlet weak var channelNameLabel: UILabel!
@IBOutlet weak var thumbnailImageView: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: nibName, bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
}
如您所见,我在编辑器视图中创建了一些 IBOutlets。一切正常。
只是有些事情我不明白。我在这个 class 中以编程方式加载 nib,那么为什么我可以创建 IBOutlets 而 Xcode 并不知道我真的会加载正确的 nib 文件?很快,我不明白 IBOutlets 在这种情况下如何工作。那么,Xcode 如何使用 IBOutlets 更正 link setup() 方法中加载的 UIView 呢?
Nib 文件有一个 "File's Owner" 类型,编辑器使用它来列出可用的出口和操作。但是,当在运行时加载 nib 时,不会强制执行该类型。插座使用 -setValue:forKey:
连接,假设传递给 instantiateWithOwner
的 "Owner" 与 nib 中定义的任何插座绑定兼容。
One xib File with Multiple "File's Owner"s
If you have an IBOutlet, but not a property, is it retained or not?