0

I have the method to check when the back button in navigation bar is press and the method go back to root page but for some reason when self.navigationController?.popToRootViewController(animated: true) it only go back to the previous page. do anyone know how to go back to the root when navigation bar back button is pressed?

override func didMove(toParentViewController parent: UIViewController?) {
    super.didMove(toParentViewController: parent)

    if parent == nil{
        self.navigationController?.popToRootViewController(animated: true)

    }

}

In this question he is asking how to what method can he use to customise his back button. In my code its able to detect when user press on back button and self.navigationController?.popToRootViewController(animated: true) is suppose to bring the page back to the root page, however there are somethings in the system preventing my app to go back to the root page.

KUROYUKI
  • 113
  • 4
  • 16
  • Possible duplicate of [Execute action when back bar button of UINavigationController is pressed](https://stackoverflow.com/questions/27713747/execute-action-when-back-bar-button-of-uinavigationcontroller-is-pressed) – Scriptable Aug 21 '18 at 14:26

2 Answers2

5

i think the best way is to create your own custom back button at this page

override func viewDidLoad {
    super.viewDidLoad()
    navigationItem.hidesBackButton = true
    let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.back(sender:)))
    navigationItem.leftBarButtonItem = newBackButton
}

func back(sender: UIBarButtonItem) {
    // Perform your custom actions
    // ...
    // Go back to the root ViewController
    _ = navigationController?.popToRootViewController(animated: true)
}

credit to this answer by 'fr33g' : Execute action when back bar button of UINavigationController is pressed

Arie Pinto
  • 1,274
  • 1
  • 11
  • 19
0

Personally I would not recommend what you are trying to achieve, but anyways here is a different solution without customizing the back button.

Steps to implement

  1. Create CustomNavigationController by subclassing UINavigationController
  2. Override popViewController(animated:)
  3. When ViewController conforms to Navigationable and
    • shouldCustomNavigationControllerPopToRoot() returns true, call super.popToRootViewController
    • Otherwise proceed with normally popping the ViewController

Source Code

Custom Navigation Controller

import UIKit

class CustomNavigationController: UINavigationController {

    // MARK: - Initializers

    override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)

        initialSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        initialSetup()
    }

    // MARK: - Setups

    private func initialSetup() {
        // DISCLAIMER: This code does not support `interactivePopGestureRecognizer`, therefore we disable it
        interactivePopGestureRecognizer?.delegate = nil
    }

    // MARK: - Overrides

    override func popViewController(animated: Bool) -> UIViewController? {
        if shouldNavigationPopToRoot {
            return super.popToRootViewController(animated: animated)?.last
        }

        return super.popViewController(animated: animated)
    }

    // MARK: - Helpers

    private var shouldNavigationPopToRoot: Bool {
        return (topViewController as? Navigationable)?.shouldCustomNavigationControllerPopToRoot() == true
    }
}

View Controller conforming to Navigationable

import UIKit

protocol Navigationable: class {
    func shouldCustomNavigationControllerPopToRoot() -> Bool
}

class ViewController: UIViewController, Navigationable {

    // MARK: - Protocol Conformance
    // MARK: Navigationable

    func shouldCustomNavigationControllerPopToRoot() -> Bool {
        return true
    }
}

Output

CustomNavigationController

E-Riddie
  • 14,660
  • 7
  • 52
  • 74
  • it dose work however this method will change all of the back button is there anyway to change selected back button? – KUROYUKI Aug 22 '18 at 10:50
  • @KUROYUKI Yes it is possible via a protocol, I will update the answer! – E-Riddie Aug 22 '18 at 11:06
  • Hi @EridB now when I am trying to use the same code on swift 4.2 its giving me an "Command CompileSwiftSources failed with a nonzero exit code" error do you know why is this happening and how can I fix it? – KUROYUKI Nov 02 '18 at 02:34