0
votes

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

2
Is the FinalController also a part of PageViewController?PGDev
@ PGDev, no this is separate view controlleriOS
FinalController is independent view controlleriOS

2 Answers

0
votes

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

0
votes

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)