1
votes

I have UIScrollView which contains many following Subscription View Controllers. Each Subscription View Controller contains Container View Controller.

Image

The target is to do a simple navigation between 4 Views Controllers at the right side.

Navigation logic:

  • On viewDidLoad show first or second View Controller in Container
  • When the user press a button on View Controller show the third View Controller in the Container

I tried to use Segues but this didn't work. The way to instantiate VCs to Subscription View Controller is not good idea.

2

2 Answers

0
votes

Use this code to switch the container views view...

@IBOutlet weak var container: UIView!
   var currentViewController:UIViewController?

//put the view did load method here.



    @IBAction func buttonPressed() {

//the storyboard file that the view is in.
        let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)


// the container view you want to switch to.
   self.currentViewController = storyboard.instantiateViewControllerWithIdentifier("containerViewStoryboardID") as? UIViewController


//switch the container view.
        self.addChildViewController(self.currentViewController!)

        self.container.addSubview(self.currentViewController!.view)

        self.currentViewController!.didMoveToParentViewController(self)


}
0
votes

The simplest way I found is to create custom Segue. enter image description here

  1. Creating custom UIStoryboardSegue

    1. Go to File -> New -> File... and choose Cocoa Class

    enter image description here

    1. Create a new class from UIStoryboardSegue

    enter image description here

    1. Configurate MySegue
import UIKit

class NewSegue: UIStoryboardSegue {

    //Call when performSegueWithIdentifier() called
    override func perform() {
        //ViewController segue FROM
        var sourceViewController: UIViewController = self.sourceViewController as! UIViewController
        //ViewController segue TO
        var destinationViewController: UIViewController = self.destinationViewController as! UIViewController
        //Parent ViewController - ContainerViewController
        var containerViewController: UIViewController = sourceViewController.parentViewController!

        //Setting destinationViewController
        containerViewController.addChildViewController(destinationViewController)
        destinationViewController.view.frame = sourceViewController.view.frame
        sourceViewController.willMoveToParentViewController(nil)

        //Do animation
        containerViewController.transitionFromViewController(sourceViewController,
            toViewController: destinationViewController,
            duration: 0.3,
            options: UIViewAnimationOptions.TransitionCrossDissolve,
            animations: nil, completion: { finished in
                //Delete sourceViewController
                sourceViewController.removeFromParentViewController()
                //Show destinationViewController
                destinationViewController.didMoveToParentViewController(containerViewController)
        })
    }
}
  1. Go to your Storyboard file and do control drag from ContainerViewController to needed Controller and choose Custom in context menu

enter image description here

3.Click on created segue and configure them

enter image description here

  1. Now you can call performSegueWithIdentifier("SugueID", sender: self) in your ContainerViewController or in other ViewController