ios Swift - 如何将两个视图控制器链接到一个容器视图中并使用分段控件在它们之间切换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28003602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift - How to link two view controllers into one container view and switch between them using segmented control?
提问by Mohammad Nurdin
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.
我有一个包含 1 个分段控件和 2 个 UI 视图的视图控制器。但是我认为更新 UI 视图以进行增强以供将来编辑太复杂了。我正在使用隐藏方法。
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.
}
}
What I want is 1 container view that contain 2 controller views so I can switch them using segmented control.
我想要的是包含 2 个控制器视图的 1 个容器视图,因此我可以使用分段控制来切换它们。
回答by Rob
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)
})
}
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.
显然,这假设您已经在视图中获得了第一个子视图控制器(如果您使用 Interface Builder 中的“容器视图”控件很容易完成)和UISegmentedControl
匹配的默认选择值。您还必须拥有这两个子场景的故事板标识符。
For Swift 2 rendition, see previous revision of this answer.
对于 Swift 2 版本,请参阅此答案的先前修订版。
回答by scott
I tried extensively to do this a while ago. For some reason the hidden properties wouldn't work for me, and the container view doesn't seem to update. I know it's not the ideal solution, but I ended up creating two container views and using the segmented control to change the alphas of the container views. Again, not ideal, but it worked nicely.
不久前,我曾广泛尝试这样做。由于某种原因,隐藏的属性对我不起作用,并且容器视图似乎没有更新。我知道这不是理想的解决方案,但我最终创建了两个容器视图并使用分段控件来更改容器视图的 alpha。同样,不理想,但效果很好。
回答by Sai kumar Reddy
import UIKit
class ContactsView: UIViewController {
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBOutlet weak var privateView: UIView!
@IBOutlet weak var publicView: UIView!
@IBAction func segmentChanged(sender: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex
{
case 0:
privateView.hidden = false
publicView.hidden = true
case 1:
privateView..hidden = true
publicView.hidden = false
default:
break;
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}