Pages

Switching Views Without using Segues in Swift

In multi View application you need to create segues manually to switch among different views on the storyboard as well as assigning the different identifiers if you want to present different views from the same view by clicking on different portions or buttons of the Current View. Now what if you there are many views on the storyboard the creating segues will be an overhead to you as well && probably you will make some mistakes in creating leagues to different views.

To overcome this problem you can switch among different views programmatically.The advantage of this approach is that you don't need to create segues manually on the storyboard && you can switch among as many views as you need to without any extra overhead of assigning identifiers to the segues of storyboard.


Now consider an example of switching between two views without creating segues. Let name of the first view is "samePageViewController" && "differentPageViewController" for the second && you will have a main class whose task is to switch these two views. To do this task you will be instantiating the storyboard view with their identifiers. To do we use instantiateViewControllerWithIdentifier() method. Here is the complete code that will allow you to switch between two views:

//creating a new differentPageViewController, if required
if differentPageViewController?.view.superview == nil {
 if differentPageViewController == nil {
    differentPageViewController = storyboard?.instantiateViewControllerWithIdentifier("Different") as DifferentPageViewController
 }
} else if samePageViewController?.view.superview == nil {
    if samePageViewController == nil {
     samePageViewController = storyboard?.instantiateViewControllerWithIdentifier("Same") as SamePageViewController
 }
}
  //checking if same page is superview or not. If yes, then Switch
if samePageViewController != nil && samePageViewController?.view.superview != nil {
  differentPageViewController.view.frame = view.frame
   switchViewController(from: samePageViewController, to: differentPageViewController)
        
} else if differentPageViewController != nil && differentPageViewController?.view.superview != nil {
  samePageViewController.view.frame = view.frame
   switchViewController(from: differentPageViewController, to: samePageViewController)        
}

---> Here in this code switchViewController() method is user defined method that looks like this:


private func switchViewController (from fromVC: UIViewController? , to toVC: UIViewController?) {
                                    //switching between different View Controllers
        if fromVC != nil {
            fromVC!.willMoveToParentViewController(nil)
            fromVC!.view.removeFromSuperview()
            fromVC!.removeFromParentViewController()
        }
        if toVC != nil {
            self.addChildViewController(toVC!)
            self.view.insertSubview(toVC!.view, atIndex: 0)
            toVC!.didMoveToParentViewController(self)
        }

    }

In this function "fromVC"is the object that represents which View Controller is present at the moment && from this view Controller you will switch.&& "toVC" is the object that represents the view controller that will be presented or that replace the current View.

Well guys go ahead && try it out. It is worth trying.


No comments:

Post a Comment