我有一个 UITableViewCell
(.xib),在这个单元格中我有一个按钮,按下按钮时我想打开一个 UIViewController
。
图标是我的按钮
在我的 TableViewController
中:
class DetalhaConsultaServidorViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
let cell = Bundle.main.loadNibNamed("HistoricoClienteServidorTableViewCell", owner: self, options: nil)?.first as! HistoricoClienteServidorTableViewCell
...
return cell
}
和我的 CustomCell
类:
class HistoricoClienteServidorTableViewCell: UITableViewCell {
@IBAction func actionButton(_ sender: Any) {
//how open a UIViewController (xib or main.storyboard)?
}
...
如何打开一个 UIViewController
(xib 或 main.storyboard)?
谢谢!
最佳答案
你可以为这个用例使用delegate
模式,
你的tableViewCell
:
protocol HistoricoClienteServidorTableViewCellDelegate {
func didButtonPressed()
}
class HistoricoClienteServidorTableViewCell: UITableViewCell {
var delegate: HistoricoClienteServidorTableViewCellDelegate?
@IBAction func actionButton(_ sender: Any) {
delegate?.didButtonPressed()
}
}
您的ViewController
:
class DetalhaConsultaServidorViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
let cell = Bundle.main.loadNibNamed("HistoricoClienteServidorTableViewCell", owner: self, options: nil)?.first as! HistoricoClienteServidorTableViewCell
cell.delegate = self
return cell
}
extension DetalhaConsultaServidorViewController: HistoricoClienteServidorTableViewCellDelegate {
func didButtonPressed() {
// Push or present your view controller
}
}
谢谢。
关于ios - Swift - 如何从 UITableViewCell 中的按钮调用 UIVIewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48334292/