[英]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.