ios - 4 个 View Controller 之间的简单自定义导航

标签 ios swift cocoa-touch uiviewcontroller

我有 UIScrollView,其中包含许多以下订阅 View Controller 。每个订阅 View Controller 都包含容器 View Controller

Image

目标是在右侧的 4 个 Views Controllers 之间进行简单导航。

导航逻辑:

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

我尝试使用 Segues 但这没有用。实例化VC的方式 订阅 View Controller 不是个好主意。

最佳答案

使用此代码切换容器 View 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)


}

关于ios - 4 个 View Controller 之间的简单自定义导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31210799/

相关文章:

swift - 使 Swift 协议(protocol)在关联类型上符合 Equatable

iphone - 键/值更新字符串值

iphone - 我的 iPhone 应用程序被存档为 "Mac App Archive"而不是 "iOS app archive"

ios - 砌体 Storyboard

math - CGPoint 标量乘法 Swift

swift - SWRevealViewController 和 Swift 的正确使用方法是什么?

ios - NSDate - 设置 future 日期

iphone - 删除 UITableViewCell 和该单元格的所有数据

ios - 如何根据单元格内容更改单元格高度?

ios - Objective-C 将对象放入每个数组