Search code examples
iosswiftxcodeuinavigationcontrolleruipageviewcontroller

UIPageViewController change navigation bar back button title


This is my complete code . In PageViewController navigation not working

Here when I click button in view controller of page view controller , it will navigate to next view (Final View Controller). Now i want to change the navigation bar back button .

enter image description here

See in above image when i click button in ViewController [View controller will load in PageViewController] it will navigate to FinalViewController

My code for navigation bar title ViewController:

@IBAction func btn(_ sender: Any) {
    print("ViewController one")
    print(variable)

    let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "FVC")
    let backItem = UIBarButtonItem()
    backItem.title = "Final VC"
    //        self.navigationItem.backBarButtonItem = backItem

    (UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.self.navigationBar.backItem?.backBarButtonItem = backItem

    (UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.pushViewController(storyboard!, animated: true)


}

But this code not working


Solution

  • This answer working fine...

    let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "FVC")
    let backButton = UIBarButtonItem()
    backButton.title = "name"
    (UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.self.navigationBar.topItem?.backBarButtonItem = backButton
    (UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.pushViewController(storyboard!, animated: true)