我有一个父类A,它有两个容器B和C,带有两个相应的uiviewcontrollers。我正在使用UISegmentContol切换两个控制器。父类有一个对象"变量"我想传递给UiViewControllers B和C.
我的DouBt: 我如何从父类共享一个对象到两个子类? 如何在父类中创建子类的引用?
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时都不会创建新实例。
答案 0 :(得分:1)
您必须在某处编写此代码以导航到所需的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
}
}
如果代码不起作用,还会调试一些断点来检查代码,检查是否正在调用函数和正确分配变量。