我在命名为仪表板控制器的视图控制器之一中使用三索引分段控件,并将segue传递到容器视图以在仪表板控制器中加载三个不同的视图控制器,但所选索引仍显示分段控件但容器视图将默认的第一个视图控制器加载到仪表板控制器下。
答案 0 :(得分:0)
您可以使用此链接,它会帮助您解决问题。
http://codingdiscovery.blogspot.com/2015/03/swap-viewcontrollers-with-segmented.html
![And fill in the functionality of the value changed method to hide/show the blue container based on the segment index: ] 1
答案 1 :(得分:0)
这就是我的方法。
func switchViews(_ sender: Any?) {
// vcSelector is an instance variable, or you can use the sender
let subVCIndex = vcSelector?.selectedSegmentIndex
// also an instance variable
currentViewController = nil
switch subVCIndex {
case 0:
currentViewController = self.storyboard!.instantiateViewController(withIdentifier: "SubControllerA")
// set up for case 0
case 1:
currentViewController = self.storyboard!.instantiateViewController(withIdentifier: "SubControllerB")
// set up for case 1
// ...
default:
// should never happen
NSLog("*** Invalid view mode! ***")
return // bail out
}
// add the new view controller
self.addChildViewController(currentViewController!)
// take out all the old subviews
for subview in self.containerView!.subviews {
subview.removeFromSuperview()
}
// install the new subVC's view
self.containerView?.addSubview(currentViewController!.view)
currentViewController!.view.frame = self.containerView!.bounds
}