![](/img/trans.png)
[英]Using Swift, how do I call a function coded in one view controller from a button in another view controller?
[英]How do I get the Segmented Control index from one view controller to another containerview controller in Swift iOS development?
我在命名為儀表板控制器的視圖控制器之一中使用三個索引分段控件,並將segue傳遞到容器視圖以在儀表板控制器中加載三個不同的視圖控制器,但所選索引仍顯示分段控件,但容器視圖正在加載儀表板控制器下的默認第一視圖控制器。
這是我的方法。
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.