ios - 通过 UISegmentControl 将数据传递给 childViewControllers

标签 ios uiviewcontroller swift2 parameter-passing uisegmentedcontrol

我有一个父类 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/

相关文章:

ios - 什么时候可以通过将代码放在 AppDelegate 中来减少代码重复?

ios - 按钮单击事件(UIButton TouchUpInside 操作)不起作用 - IOS/Swift

ios - 从 Parent ViewController 访问包含的 UIView 组件

ios - 正确使用多个'If'语句?

ios - 以编程方式振动 Myo Armband

html - 媒体查询 - iOS 设备

swift - 随着屏幕尺寸的增加,触摸屏在屏幕上形成的路径正在缩小

swift - 如何重写 Swift++ 运算符? : ternary operator

javascript - 在 iOS 中使用 Javascript 模块

iPhone:UIImageView 淡入 - 如何?