在 Swift 中使用 xib 创建 UIView
create UIView with xib in Swift
我想用 xib 创建自定义视图,
所以我做了 CreateTask.xib 包含一个 TextField
class 是 CustomUIView: UIView
我按照这一步从
http://swiftdeveloperblog.com/creating-custom-user-interface-files-with-xib-in-xcode-6-and-swift/
import UIKit
class CreateTaskView: UIView {
@IBOutlet weak var taskNameField: UITextField!
var nibView: UIView!
var taskName: String? {
get {
if taskNameField == nil {
return "taskNameField is nil"
}
if let s = taskNameField.text {
return s
} else {
return ""
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
print("infinite loop")
super.init(coder: aDecoder)
setup()
}
func setup() {
nibView = loadViewFromNib()
nibView.frame = bounds
nibView.autoresizingMask = [UIViewAutoresizing.flexibleWidth ,UIViewAutoresizing.flexibleHeight]
addSubview(nibView)
}
func loadViewFromNib() -> UIView {
let nib = UINib(nibName: "CreateTask", bundle: nil)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
}
当我实例化CreateTaskView时,那个CreateTaskView是被loadViewFromNib()无限创建的,我想这可能是"xib class is CustomUIView"的原因,所以我把它去掉了。
但是我现在无法在 xib 中获取 UITextField 插座,我该怎么办?
从 class 本身创建 nib 不是一个好主意,创建自定义视图实例的 class 应该使用其 nib 文件加载视图。一个 Objective-C 例子:
CustomView *myView = (CustomView *)[[[NSBundle mainBundle] loadNibNamed:@"CustomViewXIB" owner:nil options:nil] firstObject];
它将使用其对应的 XIB 文件创建 CustomView
class 的实例。
- 转到
CreateTask.xib
并点击“文件所有者”选项卡
- 通过点击
Identity Inspector
设置 xib 的所有者并将自定义 class 设置为 CreateTaskView
- 然后尝试将文本字段的出口创建到
CreateTaskView
我想用 xib 创建自定义视图, 所以我做了 CreateTask.xib 包含一个 TextField class 是 CustomUIView: UIView
我按照这一步从 http://swiftdeveloperblog.com/creating-custom-user-interface-files-with-xib-in-xcode-6-and-swift/
import UIKitclass CreateTaskView: UIView {
@IBOutlet weak var taskNameField: UITextField! var nibView: UIView! var taskName: String? { get { if taskNameField == nil { return "taskNameField is nil" } if let s = taskNameField.text { return s } else { return "" } } } override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder aDecoder: NSCoder) { print("infinite loop") super.init(coder: aDecoder) setup() } func setup() { nibView = loadViewFromNib() nibView.frame = bounds nibView.autoresizingMask = [UIViewAutoresizing.flexibleWidth ,UIViewAutoresizing.flexibleHeight] addSubview(nibView) } func loadViewFromNib() -> UIView { let nib = UINib(nibName: "CreateTask", bundle: nil) let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView return view }
}
当我实例化CreateTaskView时,那个CreateTaskView是被loadViewFromNib()无限创建的,我想这可能是"xib class is CustomUIView"的原因,所以我把它去掉了。
但是我现在无法在 xib 中获取 UITextField 插座,我该怎么办?
从 class 本身创建 nib 不是一个好主意,创建自定义视图实例的 class 应该使用其 nib 文件加载视图。一个 Objective-C 例子:
CustomView *myView = (CustomView *)[[[NSBundle mainBundle] loadNibNamed:@"CustomViewXIB" owner:nil options:nil] firstObject];
它将使用其对应的 XIB 文件创建 CustomView
class 的实例。
- 转到
CreateTask.xib
并点击“文件所有者”选项卡 - 通过点击
Identity Inspector
设置 xib 的所有者并将自定义 class 设置为CreateTaskView
- 然后尝试将文本字段的出口创建到
CreateTaskView