Swift - 如何将两个视图控制器链接到一个容器视图中,并使用分段控件在它们之间切换? [英] Swift - How to link two view controllers into one container view and switch between them using segmented control?

查看:151
本文介绍了Swift - 如何将两个视图控制器链接到一个容器视图中,并使用分段控件在它们之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个视图控制器,包含1个分段控件和2个UI视图。但我认为更新UI视图以进行增强以供将来编辑太复杂了。我正在使用隐藏方法。

Right now I got one view controller that contain 1 segmented control and 2 UI views. But I think it's too complicated to update the UI view for enhancement for future editing. I'm using hidden method.

import UIKit

class PopularHistoryViewController: UIViewController {


    @IBOutlet weak var segmentedControl: UISegmentedControl!
    @IBOutlet weak var popularView: UIView!
    @IBOutlet weak var historyView: UIView!

    @IBAction func indexChanged(sender: UISegmentedControl) {
        switch segmentedControl.selectedSegmentIndex
        {
        case 0:
            NSLog("Popular selected")
            //show popular view
            popularView.hidden = false
            historyView.hidden = true
        case 1:
            NSLog("History selected")
            //show history view
            popularView.hidden = true
            historyView.hidden = false
        default:
            break; 
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

}

我想要的是1个容器视图包含2个控制器视图,因此我可以使用分段控件切换它们。

What i want is 1 container view that contain 2 controller views so I can switch them using segmented control.

请提供建议。

推荐答案

另一种方法是一次只在内存中有一个子视图控制器,然后在分段控件中更改选定的值时,加载新的子视图控制器,在一个子视图控制器之间转换到下一个子视图控制器,然后删除旧的子视图控制器:

The other approach is to only have one child view controller in memory at one time, and then upon changing the selected value in the segmented control, load the new child view controller, transition between one child view controller to the next, and then remove the old child view controller:

let viewControllerIdentifiers = ["first", "second"]  // storyboard identifiers for the child view controllers

@IBAction func didChangeValue(sender: UISegmentedControl) {
    let newController = storyboard!.instantiateViewController(withIdentifier: viewControllerIdentifiers[sender.selectedSegmentIndex])
    let oldController = childViewControllers.last!

    oldController.willMove(toParentViewController: nil)
    addChildViewController(newController)
    newController.view.frame = oldController.view.frame

    transition(from: oldController, to: newController, duration: 0.25, options: .transitionCrossDissolve, animations: {
        // nothing needed here
    }, completion: { _ -> Void in
        oldController.removeFromParentViewController()
        newController.didMove(toParentViewController: self)
    })
}

显然,这假设您已经在视图上已经有了第一个子视图控制器(如果在Interface Builder中使用容器视图控件,则很容易完成)和<$ c $的默认选择值c> UISegmentedControl 匹配。您还必须具有这两个子场景的故事板标识符。

Obviously, this assumes that you've already got the first child view controller already on the view (easily done if you use the "container view" control in Interface Builder) and the default selected value for the UISegmentedControl matches. You also have to have storyboard identifiers for these two child scenes.

对于Swift 2再现,请参阅此答案的上一版本

For Swift 2 rendition, see previous revision of this answer.

这篇关于Swift - 如何将两个视图控制器链接到一个容器视图中,并使用分段控件在它们之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆