4个视图控制器之间的简单定制导航

时间:2015-07-03 16:09:37

标签: ios swift cocoa-touch uiviewcontroller

我有 UIScrollView ,其中包含许多订阅视图控制器。每个订阅视图控制器包含容器视图控制器

Image

目标是在右侧的4个视图控制器之间进行简单导航。

导航逻辑:

  • viewDidLoad 上显示容器
  • 中的第一个或第二个 View Controller
  • 当用户按下 View Controller 上的按钮时,在容器
  • 中显示第三个 View Controller

我尝试使用 Segues ,但这不起作用。实例化VC的方法 订阅视图控制器不是一个好主意。

2 个答案:

答案 0 :(得分:0)

使用此代码切换容器视图视图...

@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)


}

答案 1 :(得分:0)

我发现最简单的方法是创建自定义 Segue enter image description here

  1. 创建自定义UIStoryboardSegue

    1. 转到文件 - > - > 文件... 并选择 Cocoa Class
    2. enter image description here

      1. UIStoryboardSegue
      2. 创建新课程

        enter image description here

        1. 配置 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. 转到 Storyboard 文件并控制从 ContainerViewController 拖动到所需的控制器,然后在上下文菜单中选择自定义
        2. enter image description here

          3.点击创建的segue并配置它们

          enter image description here

          1. 现在,您可以在 ContainerViewController 或其他 ViewController
          2. 中调用performSegueWithIdentifier("SugueID", sender: self)