通过 UI Segment Control 将数据传递给子视图控制器

Pass Data to childViewControllers via UISegmentControl

我有一个父 class A 有两个容器 B 和 C,分别有两个 uiviewcontroller。我正在使用 UISegmentContol 切换两个控制器。父 class 有一个对象 "Variables",我想将其传递给 UiViewControllers B 和 C。

我的疑问: 我如何将对象从父 class 共享给两个子 classes? 我如何在父项 class 中创建我的子项 classes 的引用?

class A: UIViewController {

@IBOutlet weak var segment: UISegmentedControl!
@IBOutlet var containerSearch: UIView!
@IBOutlet var containerAdvancedSearch: UIView!
var variables: Variables?

override func viewDidLoad() {
    super.viewDidLoad()
    let a = variables?.bankNameLong
    print("from " + a! + "")
}

@IBAction func actionSwitchSegments(sender: AnyObject) {
   if sender.selectedSegmentIndex == 0 {
        UIView.animateWithDuration(0.5, animations: {
            self.containerChangePassword.alpha = 1
            self.containerAbout.alpha = 0
        })
    } else {
        UIView.animateWithDuration(0.5, animations: {
            self.containerChangePassword.alpha = 0
            self.containerAbout.alpha = 1
        })
    }

}



   if segue.identifier == "segSearch"{
        let newViewController: vConSearch = segue.destinationViewController as! vConSearch
        newViewController.Variables = self.Variables
    }else if segue.identifier == "segAdvancedSearch"{
        let newViewController: vConAdvancedSearch = segue.destinationViewController as! vConAdvancedSearch
        newViewController.Variables = self.Variables
    }

我尝试使用 segue,但它仅适用于单个子控制器。 但是通过 segue 每次我点击 UiSegmentControl 时它不会创建一个新实例吗?

您必须在某处编写此代码以导航到所需的 viewcontroller

self.performSegueWithIdentifier("segSearch", sender: sender)

然后是代表

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {

    if segue.identifier == "segSearch"{
        let newViewController: vConSearch = segue.destinationViewController as! vConSearch
        newViewController.Variables = self.Variables
    }else if segue.identifier == "segAdvancedSearch"{
        let newViewController: vConAdvancedSearch = segue.destinationViewController as! vConAdvancedSearch
        newViewController.Variables = self.Variables
    }
}

还打了一些断点来调试代码,如果它不起作用,检查被调用的函数和变量是否被正确分配。