user3628240
user3628240

Reputation: 927

HeaderView Button Segue to Another View Controller Programmatically

I have a prototype header view with a button that I want to be able to click in order to segue to another view controller (TargetViewController).

However, I haven't been able to get it to work for some reason. I am trying to do this all programmatically, so there is no prepareForSegue function like I am normally used to. Could you tell me what I'm doing wrong?

Do I still need the addTarget if using delegates? And I think the biggest issue is I can't figure out which class I should set the delegate equal to .

I'm still pretty new at this, so any help would be really appreciated!

The relevant lines of code are below:

protocol HeaderCellDelegate: class {
    func didTapEdit()
}

class HeaderCell: UITableViewHeaderFooterView {
    var editButton: UIButton!
    weak var delegate: HeaderCellDelegate?


    override init(reuseIdentifier: String?) {
        self.editButton = UIButton()
        self.editButton.setImage(UIImage(named: "Edit"), for: .normal)

        super.init(reuseIdentifier: reuseIdentifier)

        self.editButton.addTarget(self, action: #selector(editButtonPressed), for: .touchUpInside)

        self.contentView.addSubview(editButton)

        // Plaeholder for constraints
    }

    required init?(coder aDecoder: NSCoder) {
         fatalError()
    }

    @objc func editButtonPressed() {
        delegate?.didTapEdit()
        print("delegate didTapEdit")
    }
}

Table View controller:

extension ProfileDataController: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return tableViewSections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableViewSections[section].rows.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {    
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
        return header
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {    
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UITableViewCell
        return cell
    }
}

Target view controller:

import UIKit

extension TargetViewController: HeaderCellDelegate {

    func didTapEdit() {
        print("editButtonPressed")
    }
}

Upvotes: 0

Views: 681

Answers (3)

jignesh Vadadoriya
jignesh Vadadoriya

Reputation: 3310

You need to give HeaderCellDelegate delegate like header.delegate = self

extension ProfileDataController: UITableViewDataSource, UITableViewDelegate,HeaderCellDelegate {

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
       header.delegate = self
        return header
    }
}

Then implement delete method in ProfileDataController and by using performSegue method you can execute your Segue

//HeaderCellDelegate
extension ProfileDataController  {

  func didTapEdit() {
        print("editButtonPressed")
      self.performSegue(withIdentifier: "YOUR SEGUE IDENTIFIRE", sender: nil)
    }
}

Upvotes: 0

vp2698
vp2698

Reputation: 1805

Without delegates you can add action in ProfileDataController

extension ProfileDataController: UITableViewDataSource, UITableViewDelegate {
   func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
        header.editButton.addTarget(self, action: #selector(editButtonPressed), for: .touchUpInside)
        return header
   }
}

In ProfileDataController

@objc func editButtonPressed() {
    print("edit button pressed")
    self.performSegue(withIdentifier: "YOUR SEGUE IDENTIFIRE", sender: nil)
}

Upvotes: 2

Mahendra
Mahendra

Reputation: 8914

You need to set the delegate to the current view controller (In your case you need to set it like below)

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderCell") as! HeaderCell
header.deelgate = self
    return header
}

And then in your ProfileDataController class you need to implement HeaderCellDelegate method like below

extension ProfileDataController : HeaderCellDelegate {

  let viewControllerToBePushed = TargetViewController() //get the `TargetViewController` here...
  self.navigationController.pushViewController(viewControllerToBePushed, animated: true)
}

So TargetViewController will be pushed over ProfileDataController

Upvotes: 1

Related Questions