我有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)?
谢谢!
答案 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本身中实现。