I have a UIViewController that instantiates some UITableViewCell according to the desired option on the screen. One of theses options is "instructions", and I created a .XIB file to show the instructions on the screen. But, on this screen, I have a button to show more instruction (in another screen). So, I created another .XIB file with the instructions and I need to go there from the first instructions screen. How can I do that?
This is my code:
import UIKit
class FirstInstructionsCell: UITableViewCell {
@IBOutlet weak var showSecondInstructions: UIButton!
var secondInstructionsView: SecondInstructions!
static let FIRST_INSTRUCTIONS_CELL_IDENTIFIER = "FirstInstructionsCell"
@IBAction func showSecondInstructionsTapped(_ sender: UIButton) {
print("Here I need to go to SecondInstructions screen")
}
}
After changes, my UIViewController:
import UIKit
class FirstInstructionsViewController: UIViewController, FirstInstructionsCellDelegate {
func goSecondScreen() {
presentViewController(SecondInstructions, animated: true, completion: nil)
// Here I have an error: Cannot convert value of type 'SecondInstructions.Type' to expected argument type 'UIViewController'
// 'SecondInstructions' is a UITableViewCell
}
@IBOutlet weak var confirmButton: UIButton!
@IBOutlet weak var tableView: UITableView!
var withdrawalCode: Int!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UINib(nibName: FirstInstructionsCell.FIRST_INSTRUCTIONS_CELL_IDENTIFIER, bundle: nil), forCellReuseIdentifier: FirstInstructionsCell.FIRST_INSTRUCTIONS_CELL_IDENTIFIER)
self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.estimatedRowHeight = 470.0
}
//MARK: - Actions
@IBAction func onConfirmClicked(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
}
// MARK: - UITableViewDelegate
extension FirstInstructionsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 13.0
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 142.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return PayPaxxHeaderView.loadFromNib(CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 142.0)) as! PayPaxxHeaderView
}
}
// MARK: - UITableViewDataSource
extension FirstInstructionsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: FirstInstructionsCell.FIRST_INSTRUCTIONS_CELL_IDENTIFIER, for: indexPath) as! FirstInstructionsCell
cell.fillCell(self.withdrawalCode)
cell.delegate = self
return cell
}
}
As far as I understood, you need to go to another screen. Why not use protocol
for that?
import UIKit
protocol FirstInstructionsCellDelegate {
func goToSecondScreen()
}
class FirstInstructionsCell: UITableViewCell {
@IBOutlet weak var showSecondInstructions: UIButton!
var secondInstructionsView: SecondInstructions!
var delegate : FirstInstructionsCellDelegate?
static let FIRST_INSTRUCTIONS_CELL_IDENTIFIER = "FirstInstructionsCell"
@IBAction func showSecondInstructionsTapped(_ sender: UIButton) {
print("Here I need to go to SecondInstructions screen")
delegate?.goToSecondScreen()
}
}
Then, in your cellForRow(at:indexPath:)
you can say (after dequeueing):
cell.delegate = self
Your viewController
needs to implement it:
class FirstInstructionViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, FirstInstructionsCellDelegate {
and then, in that View Controller, implement said function:
func goToSecondScreen() {
let story = UIStoryboard(named: "SecondScreen", bundle: nil)
let viewController = story.instantiateViewController(withIdentifier: "SecondScreen")
self.navigationController?.pushViewController(viewController, animated: true)
// or
self.present(viewController, animated: true, completion: nil)
}
NOTE:
you need to set storyboard identifier in interface builder.