我有 UIScrollView ,其中包含许多订阅视图控制器。每个订阅视图控制器包含容器视图控制器。
目标是在右侧的4个视图控制器之间进行简单导航。
导航逻辑:
我尝试使用 Segues ,但这不起作用。实例化VC的方法 订阅视图控制器不是一个好主意。
答案 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 。
创建自定义UIStoryboardSegue
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)
})
}
}
3.点击创建的segue并配置它们
performSegueWithIdentifier("SugueID", sender: self)
醇>