ios - 从 Swift 中的其他类读取数组

标签 ios swift

我需要从 ViewController-B 读取 ViewController-A 中的数组,以便填充 ViewController-B 中的 UIPickerView 的数据。我该怎么做?

我试过这个答案:

https://stackoverflow.com/a/31574620/7127489

但我不知道为什么我不能像它所说的那样实例化我的 ViewController-B,以便将我的填充数组传递给另一个类中的空数组。

有什么想法吗? 非常感谢!

最佳答案

您可以使用委托(delegate)来传递数据(SO 上有许多委托(delegate)示例)或 here 或者你可以这样做:

如果在导航 Controller 中:

if let navController = self.tabBarController?.viewControllers?[1] as? UINavigationController, let yourController = navController.viewControllers[0] as? yourVC{ 
    yourArray = yourController.array
}

如果不在navigationController中:

if let yourController = self.tabBarController?.viewControllers?[1] as? yourVC{ 
    yourArray = yourController.array
}

关于ios - 从 Swift 中的其他类读取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45434453/

相关文章:

ios - Apple Watch openparentapplication : reply: receives Error Domain=FBSOpenApplicationErrorDomain Code=5

arrays - 检查数组的所有元素是否在 Swift 中具有相同的值

macos - 打开 sqlite 文件 swift osx 应用程序并读取数据

ios - 以关联类型作为属性的协议(protocol)

swift - 在 Swift 中使用 CFArrayGetValueAtIndex 和 UnsafePointer (AUPreset)

ios - 根据 NSString 值的匹配从 NSMutableDictionary 中删除条目

iphone - 按类名查询 Google for iOS SDK API 文档会从 Mac OS X 库返回指向该类的链接。为什么?我怎样才能避免这种情况?

ios - 在 ARKit 中隐藏环境后面的地板

ios - UIButton 的区别!和 UI 按钮?在 swift IOS 中

ios - 如何使用 swift 将图像从 Images.xcassets 加载到 UIImage