skip to Main Content

I have a UIViewController with a Container View. The container view has a navigation controller with which I can display different scenes within the main view.

Structure:

MainVC
1SubVC (Container View) inside MainVC
2SubVC (ContainerView) inside MainVC

Now my question: Can I execute a function from my MainVC so that the 1SubVC pushes to the 2SubVC?

Something like ->

class MainVC {
   1SubVC().doThisPushFunc()
}

Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    Update

    For everyone who is looking for a similar solution.

    The solution was the NotificationCenter.

    1. Listener für Notification in SubView1 in ViewDidLoad

    2. objc func in SubView1 to execute the notification

    3. Call the notification from MainVC


  2. By saying "push" I’m assuming you’re going to use a UINavigationController and push a view controller onto the navigation stack. The main reason you would use a UINavigationController class is to have helpful functionality like the animation when you’re pushing to a new view controller or if you’re needing a top navigation bar.

    If you’re just wanting to go from showing 1SubVC to 2SubVC, then replace the container view.
    If you’re wanting to have MainVC notify 1SubVC to make the navigationController.pushViewController() call then access an instance of 1SubVC() so you can call the push method
    e.g. Inside of MainVC

        let oneSubVC = 1SubVC()
        oneSubVC.navigationController.pushViewController(ViewController(), true)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search