用XIB1創建的UIView我想創建一個自定義視圖用XIB1, 所以,我沒有CreateTask.xib包含一個TextField 和類是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類是CustomUIView」的原因,所以我把它拿出來了。
但我無法在xib中獲得UITextField插座,我該怎麼辦?