I have a UITableViewCell
(.xib) and in this cell i have a Button, When pressed the button i would like to open a UIViewController
.
The icon is my button
In my 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:
class HistoricoClienteServidorTableViewCell: UITableViewCell {
@IBAction func actionButton(_ sender: Any) {
//how open a UIViewController (xib or main.storyboard)?
}
...
How open a UIViewController
(xib or main.storyboard)?
Thanks!
You can use delegate
pattern for this use case,
Your tableViewCell
:
protocol HistoricoClienteServidorTableViewCellDelegate {
func didButtonPressed()
}
class HistoricoClienteServidorTableViewCell: UITableViewCell {
var delegate: HistoricoClienteServidorTableViewCellDelegate?
@IBAction func actionButton(_ sender: Any) {
delegate?.didButtonPressed()
}
}
Your 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:
cell.actionButton.addTarget(self, action: #selector(btnOpenCaseTapped), for: .touchUpInside)
Add UIButton Action in DetalhaConsultaServidorViewController ViewController like below:
@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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.