ios - 将相同的变量传递给具有不同值的容器 View

标签 ios swift dropdown uicontainerview

我在主 viewController 上有一个按钮,它具有默认值 nil,它由 dropDown 关联荚。 在同一个 viewController 上还有一个容器 View 。

在第一次加载时,我从共享首选项中获取变量的默认值,并通过 performSegue 将该值传递给容器 View 。

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if(segue.identifier == "dataToContainerView"){
    DispatchQueue.main.async {

   var secondVC = segue.destination as! secondViewController //container viewController
        secondVC.variable = self.variable  
    }
  }
}

现在我需要通过用户从 dropdown 按钮中选择来再次传递相同变量的值。

 dropDown.selectionAction = { [unowned self] (index, item) in
        self.button.setTitle(item, for: UIControlState())
        self.variable = item
        print(item)
        self.performSegue(withIdentifier: "dataToContainerView", sender: nil)
  //performing segue to resend the new value of the variable.
    }

上面的代码在 print(item) 之前正常执行。 但是我在 performSegue 上收到以下错误。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There are unexpected subviews in the container view. Perhaps the embed segue has already fired once or a subview was added programmatically?

我应该如何在 dropDown pod 的帮助下第二次将值传递给容器 View 以覆盖第一个值?

更新:- 我需要变量值,以便我可以将它传递给容器 viewController 上的 json 解析器。容器 viewController 上的代码重新执行。

最佳答案

您需要保存对嵌入式 Controller 的引用,以便稍后再次更新。在第一个 segue 中执行此操作:

// Declare a local variable in your parent container:
var secondVC: secondViewController!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if(segue.identifier == "dataToContainerView"){
        DispatchQueue.main.async {
            self.secondVC = segue.destination as! secondViewController 
            //container viewController
            self.secondVC.variable = self.variable  
        }
    }
}

然后当您需要更新变量时,您可以直接引用它:

self.secondVC.variable = self.variable
self.secondVC.viewDidLoad()

关于ios - 将相同的变量传递给具有不同值的容器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47885305/

相关文章:

ios - 在现实世界中,Apple iOS 是否在逐个应用程序的基础上默认为仅 IPv6?

ios - 如何找出两个自定义单元格中的哪一个被选中?

css - 如何删除下拉菜单列表 <a> 中的填充?

ios - 如何在 iOS Swift 3 中以编程方式创建 UICollectionView 类而不使用 Storyboard?

CSS 修复下拉菜单未显示在显示器分辨率上

android - React Native Material 下拉菜单从 json 数据中获取 id

ios - 关于AFNetworking : how to add the response data to NSMutable Array中的异步连接

php - 从 IOS 到 php 服务的 Base64 图像

ios - 使用常量时的公共(public)初始化

swift - 我可以使 SCNBox 可点击并进行交互吗?