ios - Swift - 如何从 UITableViewCell 中的按钮调用 UIVIewController

标签 ios swift uitableview uiviewcontroller

我有一个 UITableViewCell (.xib),在这个单元格中我有一个按钮,按下按钮时我想打开一个 UIViewController

enter image description here

图标是我的按钮

在我的 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/

相关文章:

ios - 在 iOS 上使用 openCV 的高动态范围成像产生乱码输出

uitableview - 如何使用 SwiftyJSON 获取正确的 JSON 响应?

ios - fatal error : Index out range in numberOfRowsInSection

ios - 使用它的 indexPath.row 更改单元格图像

ios - uicontainerview(容器 View )内的多个 subview Controller

ios - 在 HealthKit HKStatisticsQuery 中收到 "Type of expression is ambiguous without more context"作为错误

ios - UIScrollview 有一个大 View ,还是 UITableview 有多个 View ?

ios - 如何在 Realm 数组的特定索引处插入对象?

ios - 有没有办法在 iOS 平台上的 NativeScript 应用程序中捕获和取消后退按钮事件?

ios - 比视频帧速率更快地更新 AVPlayerItem 上的 AVMutableVideoComposition