我有一个父类 A,它有两个容器 B 和 C,以及两个各自的 uiviewcontroller。我正在使用 UISegmentContol 切换两个 Controller 。父类有一个对象“Variables”,我想将其传递给 UiViewControllers B 和 C。
我的疑问: 如何将父类的对象共享给两个子类? 如何在父类中创建子类的引用?
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,但它仅适用于单个子 Controller 。 但是,通过执行 segue,每次我单击 UiSegmentControl 时,它不会创建一个新实例。
最佳答案
您必须在某处编写此代码以导航到所需的 View Controller
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
}
}
还打了一些断点来调试代码,如果它不起作用,检查被调用的函数和变量是否被正确分配。
关于ios - 通过 UISegmentControl 将数据传递给 childViewControllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36261562/