0

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

Naresh
  • 16,698
  • 6
  • 112
  • 113

2 Answers2

0

The back button belongs to the previous view controller, not the one currently presented on screen. you can use delegate or segue see this

Hoven
  • 563
  • 1
  • 5
  • 24
  • I write my code in ViewController of PageViewController , because I'm clicking button in ViewController of PageViewController, Now it's navigate to FinalViewController. That is separate ViewController . – Naresh Jul 10 '19 at 05:52
  • As I told, make a delegate method from your separate view controller to your view controller of Page View Controller and after click update your back button – Hoven Jul 10 '19 at 06:15
0

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)
Naresh
  • 16,698
  • 6
  • 112
  • 113