kee*_*een 4 inheritance iboutlet uistoryboard swift
实现以下内容(类继承)之后:
class UIViewControllerA: UIViewControllerB {
}
Run Code Online (Sandbox Code Playgroud)
如何让它UIViewControllerA
继承UIViewControllerB
IBOutlets?如何将Storyboard上的组件连接到子类UIViewControllerA
?
如果您的目标是继承IBOutlets,则应执行以下操作:
1-在超类中添加IBOutlets:
父类(UIViewController)不应直接连接到故事板上的任何ViewController,它应该是通用的。将IBOutlet添加到超类时,不应将它们连接到任何组件,而子类应该这样做。另外,您可能想对超类中的IBOutlet做一些工作(这就是为什么要应用此机制的原因)。
超级类应类似于:
class SuperViewController: UIViewController, UITableViewDataSource {
//MARK:- IBOutlets
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// setting text to the label
label.text = "Hello"
// conforming to table view data source
tableView.dataSource = self
}
// handling the data source for the tableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = "Hello!"
return cell!
}
}
Run Code Online (Sandbox Code Playgroud)
2-继承超类并连接IBOutlets:
简而言之,您的子类应类似于:
class ViewController: SuperViewController {
override func viewDidLoad() {
// calling the super class version
super.viewDidLoad()
}
}
Run Code Online (Sandbox Code Playgroud)
在情节提要中,将视图控制器分配给ViewController
(子类),然后重建项目(cmd+ b)。
现在,在选择了所需的View Controller并选择“ Connection Inspector”之后,您应该在IBOutlets部分中看到-:
您可以将它们手动连接到子类ViewController中存在的UI组件(从空圆圈拖动到组件)。他们应该看起来像:
就是这样!子类的表视图和标签应继承超类中包含的内容。
希望这会有所帮助。
归档时间: |
|
查看次数: |
2030 次 |
最近记录: |