2

I have a UITableView for which I want to implement a custom header. In the custom header I want a button which when tap pushes a new view onto the navigationController. I am having trouble accessing the navigationController in the custom header view's super's ViewController.

class CustomViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

    //setup views and whatnot
    //setup delegate and datasource


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

class CustomHeaderView : UIView {
    let customHeaderButton : UIButton = {
        let button = UIButton()
        ...
        button.addTarget(self, action: #selector(handleCustomHeaderButtonAction()), for: .touchUpInside)
        return button
    }()
    ...
    func handleCustomHeaderButtonAction(){
        //push a new view onto the CustomViewController's navigationController
        ???
}
mikemags1
  • 797
  • 6
  • 13
  • Why not create a function in your viewController and then call that function by using your controller from you're view ? or as suggested by @Silmaril Protocol will help you out. – Wolverine Oct 21 '16 at 05:09
  • 2
    Just use closure or delegate to handle tap outside (in view controller) of your header view – Silmaril Oct 21 '16 at 05:09
  • My answer to a similar thread: http://stackoverflow.com/questions/22904164/presentviewcontroller-from-custom-tablecell-in-xib/22904272#22904272 – NeverHopeless Oct 21 '16 at 05:46

3 Answers3

4

Try this :

class SecondViewController: UIViewController,ChildDelegate {


    internal func navigatToCustomViewController() {
        // Code to write push yur controller
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        let customview = customView()
        customview.delegate = self

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

// A Protocol
protocol ChildDelegate: class {
    func navigatToCustomViewController()
}

// You custom view
class customView : UIView {

    weak var delegate: ChildDelegate?

    func actionDetailClick() {
        delegate?.navigatToCustomViewController()
    }
}
Wolverine
  • 4,264
  • 1
  • 27
  • 49
0

Try this ;

 func handleCustomHeaderButtonAction(){
    let nav = UIApplication.shared.keyWindow?.rootViewController?.navigationController
  }
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

I love to use closures in such cases:

class CustomViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

    //setup views and whatnot
    //setup delegate and datasource
    private func pushController() {
         // push your controller here
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = CustomHeaderView(type: section)
        view.pushHandler = { [unowned self] in self.pushController() }
        return view
    }
}

class CustomHeaderView : UIView {

    var pushHandler: (Void -> Void)?
    let customHeaderButton : UIButton = {
        let button = UIButton()
        ...
        button.addTarget(self, action: #selector(handleCustomHeaderButtonAction()), for: .touchUpInside)
        return button
    }()
    ...
    func handleCustomHeaderButtonAction() {
        pushHandler?()
    }
}
Maksym Musiienko
  • 1,248
  • 8
  • 16