0

I'm aware that you can switch from one ViewController to another using an intermediary Class, as seen in this example.

What I want to know is, is there any way to switch to a different view controller directly from another view controller? Like, load the view in the current view controller, and release the one you're in immediately after?

Thanks.

Community
  • 1
  • 1
rottendevice
  • 2,849
  • 6
  • 30
  • 37

3 Answers3

0

Using UINavigationController is one way: [navigationController pushViewController:animated:]. The other "official" way is to display the next view as a modal view: [someVC presentModalViewController:], but this is deprecated since iOS 6.

The iOS 6 way is: - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion.

Zeke
  • 134
  • 1
  • 4
0
var currentVC:UIViewController!
let firstVC = yourViewcontroller1()
let secondVC = yourViewcontroller2()
func setupChildViewControllers(){
    self.addChildViewController(firstVC)
    self.addChildViewController(secondVC)
    self.view.addSubview(firstVC.view)
    self.currentVC = firstVC
}

func replaceController(oldVC:UIViewController,newVC:UIViewController){
    self.addChildViewController(newVC)

    self.transitionFromViewController(oldVC, toViewController: newVC, duration: 0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: nil) { (finished) -> Void in
        if finished {
            newVC.didMoveToParentViewController(self)
            oldVC.willMoveToParentViewController(nil)
            oldVC.removeFromParentViewController()
            self.currentVC = newVC
        }else{
            self.currentVC = oldVC
        }
    }
}
L.yansun
  • 45
  • 5
0

I think what you are asking for is how to retain 1 view controller.

What you can do is when you add your new View Controller, add it to your parent view controller and remove the current view controller.

vince
  • 21
  • 1