Swift - 如何从UITableViewCell中的Button调用UIVIewController

时间:2018-01-19 04:50:46

标签: 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)?

谢谢!

4 个答案:

答案 0 :(得分:4)

您可以对此用例使用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
      }

}

感谢。

答案 1 :(得分:3)

您需要在cellForRowAt方法中添加UIButton操作,如下所示:

cell.actionButton.addTarget(self, action: #selector(btnOpenCaseTapped), for: .touchUpInside)

在DetalhaConsultaServidorViewController ViewController中添加UIButton Action,如下所示:

@objc func btnOpenCaseTapped() {
    self.performSegue(withIdentifier: "YourSegueIdentifire", sender: nil)
}

答案 2 :(得分:0)

Function.InvokeAfter(@getResult(url),#duration(0,0,0,5))

尝试这个希望可能有所帮助。

答案 3 :(得分:0)

您可以在cellForRowAtIndexPath本身中声明按钮的IBAction。

在你的代码中......

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.myButton.addTarget(self, action:  #selector(yourButtonTapMethod:), forControlEvents: .TouchUpInside)

    return cell
}

func yourButtonTapMethod(sender:UIButton){
    let storyBoard: UIStoryboard = UIStoryboard(name: "Home", bundle: nil)
    let controllerInstance = storyBoard.instantiateViewController(withIdentifier:
        "ControllerStoryBoardIdentifier") as! YourControllerName
    self.sharedCalss.creatingNewTrip = true
    self.navigationController?.pushViewController(controllerInstance, animated: true)
}
}

按钮点击操作方法应该在DetalhaConsultaServidorViewController本身中实现。