0
votes

I have one view controller (without embedded in to navigation controller). Let say mainVC, from maninVC i have one button called goNext. When i tap i need to psuh my same VC mainVC with some changes in label.

So i did :

This is in app delegate :

 let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
 let exampleVC = storyBoard.instantiateViewController(withIdentifier: "MainVC")
 self.window = UIWindow(frame: UIScreen.main.bounds)
 self.window?.rootViewController = exampleVC
 self.window?.makeKeyAndVisible()

My vc :

   func pushToMainVc() {
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let loginPageView = mainStoryboard.instantiateViewController(withIdentifier: "MainVC") as! MainVC
        let rootViewController = self.window?.rootViewController as? UINavigationController
        rootViewController?.pushViewController(loginPageView, animated: true)
    }

But its not making the push. Any help would be great.

Thanks

2

2 Answers

0
votes

Why you want to push/load the mainVC again?. The mainVC is already loaded. If you want some changes in label to happen when goNext Button is pressed then, get the IBOutlet of this Label into you ViewController code. And also get the IBAction for the goNext Button in the code and when this button is pressed change the label. For example :

class mainVC: UIViewController {
    ...
    @IBOutlet weak var someLabel: UILabel!
    override func viewDidLoad() {
       super.viewDidLoad()
       someLabel.text = "TextOnLoad"
    }
    ... 
    @IBAction func goNextButtonPressed(_ sender: UIButton) {
       someLabel.text = "ChangedText"
    }
}
0
votes

You can't push the same ViewController to the NavigationController.