目标是使用自己的控制器获得少量视图(例如,可以使用PickerView,标签和搜索文本字段查看)和使用控制器创建这些控制器的主视图。
我尝试使用StackView将这些控制器添加到主视图中。控制器内部的方法成功调用,但堆栈视图为空。
儿童控制守则:
class LookupControl: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var picker: UIPickerView?
var pickerDataSource = ["t1", "t2", "t3"];
override func viewDidLoad() {
super.viewDidLoad()
picker?.dataSource = self
picker?.delegate = self
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerDataSource.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerDataSource[row]
}
}
父母控制代码:
class ContactDetailViewController: UIViewController, SubstitutableDetailViewController {
@IBOutlet var stackView: UIStackView?
override func viewDidLoad() {
super.viewDidLoad()
let lc = LookupControl(nibName: "LookupView", bundle: nil)
self.stackView?.addArrangedSubview(lc.view)
}
}
将UIViewControllers添加到StackView是否可行? iOS中最好的做法是什么?
提前致谢, 塞吉