简体   繁体   English

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

[英]Swift - How call UIVIewController from a Button in UITableViewCell

I have a UITableViewCell (.xib) and in this cell i have a Button, When pressed the button i would like to open a UIViewController . 我有一个UITableViewCell (.xib),在这个单元格中有一个Button,当按下按钮时,我想打开一个UIViewController

在此处输入图片说明

The icon is my button 图标是我的按钮

In my TableViewController : 在我的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
}

and my CustomCell class: 和我的CustomCell类:

class HistoricoClienteServidorTableViewCell: UITableViewCell {

    @IBAction func actionButton(_ sender: Any) {

        //how open a UIViewController (xib or main.storyboard)?
    }
    ...

How open a UIViewController (xib or main.storyboard)? 如何打开UIViewController (xib或main.storyboard)?

Thanks! 谢谢!

You can use delegate pattern for this use case, 您可以在此用例中使用delegate模式,

Your tableViewCell : 您的tableViewCell

protocol HistoricoClienteServidorTableViewCellDelegate {
    func didButtonPressed()
}

class HistoricoClienteServidorTableViewCell: UITableViewCell {
    var delegate: HistoricoClienteServidorTableViewCellDelegate?

    @IBAction func actionButton(_ sender: Any) {
       delegate?.didButtonPressed()
    }
}

Your ViewController : 您的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
      }

}

Thanks. 谢谢。

You need to add UIButton action in cellForRowAt method like below: 您需要在cellForRowAt方法中添加UIButton动作,如下所示:

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

Add UIButton Action in DetalhaConsultaServidorViewController ViewController like below: 在DetalhaConsultaServidorViewController ViewController中添加UIButton动作,如下所示:

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

       let storyBoard = UIStoryboard(name: "Main", bundle: nil)
       let vc = storyBoard.instantiateViewController(withIdentifier: "your view controller identifier") as! your vc 
       self.present(vc,animated:true,completion: nil)
    }

try this hope it may help. 尝试这种希望可能会有所帮助。

You can declare IBAction of button in cellForRowAtIndexPath itself. 您可以在cellForRowAtIndexPath本身中声明按钮的IBAction。

Here in your code.... 在您的代码中...

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)
}
}

Button tap action method should implement in DetalhaConsultaServidorViewController itself. 按钮轻击动作方法应在DetalhaConsultaServidorViewController本身中实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 3:从动态uitableviewcell内的按钮到uiviewcontroller的弹出式连接 - Swift 3: popover connection from button inside dynamic uitableviewcell to uiviewcontroller Swift-如何将数据从UITableViewCell类传递到UIViewController - Swift - How to Pass Data From UITableViewCell Class to UIViewController 如何从 UITableViewCell 按钮创建 UIView 单击单独的 UIViewController? - How to create a UIView from a UITableViewCell button click on a separate UIViewController? 如何在Swift中将函数从扩展名调用到UIViewController - How to call a function from an extension to UIViewController in Swift 在Swift中将自定义UITableViewCell从笔尖加载到UIViewController - Loading a Custom UITableViewCell from nib to a UIViewController in Swift iOS:UITableViewCell类中的按钮,用于在主UIViewController上调用方法 - iOS: button in UITableViewCell class to call method on main UIViewController 如何将数据从 UITableViewCell 传输到另一个 UIViewController? - How to transfer data from a UITableViewCell to another UIViewController? 如何从不同的 UIViewController 中的 UITableView 访问 UITableViewCell? - How to access UITableViewCell from UITableView in different UIViewController? 如何调用函数来从Swift中的UITableViewCell类更改viewController? - How to call function to change viewController from UITableViewCell class in Swift? 如何在Swift 3.0中从UITableViewCell按钮单击添加弹出窗口? - how to add popover segue from UITableViewCell button click in swift 3.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM