【问题标题】:Pass Data to childViewControllers via UISegmentControl通过 UISegmentControl 将数据传递给 childViewController
【发布时间】:2016-03-29 14:02:41
【问题描述】:

我有一个父类 A,它有两个容器 B 和 C,分别有两个 uiviewcontroller。我正在使用 UISegmentContol 切换两个控制器。父类有一个对象“变量”,我想将它传递给 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,但它仅适用于单个子控制器。 但是通过 segue 不会在我每次单击 UiSegmentControl 时创建一个新实例。

【问题讨论】:

  • 嘿@Darth Vader,你能帮忙吗?
  • 从哪里调用 performSegueWithIdentifier??
  • 来自父类 vConSettings
  • 我没有看到代码。你能检查一下你的prepareforsegue是否被调用。谢谢
  • 嘿@Ujjwal .....我已经更新了代码..你能看看..

标签: ios uiviewcontroller swift2 parameter-passing uisegmentedcontrol


【解决方案1】:

您必须在某处编写此代码才能导航到所需的视图控制器

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
    }
}

如果代码不起作用,也可以设置一些断点来调试代码,检查函数是否被调用,变量是否被正确分配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2021-04-09
    • 2021-07-18
    • 2010-11-29
    • 2018-02-13
    • 2012-10-27
    相关资源
    最近更新 更多