[英]Swift 3: popover connection from button inside dynamic uitableviewcell to uiviewcontroller
[英]Swift - How call UIVIewController from a Button in UITableViewCell
我有一个UITableViewCell
(.xib),在这个单元格中有一个Button,当按下按钮时,我想打开一个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
}
}
谢谢。
您需要在cellForRowAt
方法中添加UIButton动作,如下所示:
cell.actionButton.addTarget(self, action: #selector(btnOpenCaseTapped), for: .touchUpInside)
在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)
}
尝试这种希望可能会有所帮助。
您可以在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本身中实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.