ios - 通过对象将数据从父 ViewController 传递到子 UITableViewController

标签 ios swift

我有几个委托(delegate)已经在我的代码中工作了(不问如何使用它们)。我在从父 ViewController( Storyboard中的 More Information 属于类 InputViewController)获取数据到子 TableViewController(AddressTableViewController)时遇到问题) 因为我无法从此类引用父 Controller 。

TableViewController 和 map 都在 InputViewController 中。我想将数据(来自 locationManager 的反向地理定位地址)从 map 发送到 AddressTableViewController 以填充单元格文本。 TableViewControllers 通过对象(在对象库中选择)绑定(bind)到 InputViewController(主 Controller )。

Storyboard 代码:https://gist.github.com/damionjn/c148a07a39b5c27e7f78

你会看到我在这里尝试使用 InputViewController 底部的协议(protocol): https://gist.github.com/damionjn/c148a07a39b5c27e7f78#file-gistfile1-swift-L125 但我无法使用它,因为我无权访问 InputViewController,因此 mapAddressDataSource 始终为 nil。我已经尝试在 AddressTableViewControllerinit() 中创建它的一个实例并创建一个 IBOutlet,但没有任何运气。

enter image description here

最佳答案

I'm having a problem getting data from the parent ViewController (More Information in the storyboard belongs to class InputViewController) to the child TableViewController (AddressTableViewController) as I'm unable to reference the parent controller from this class.

为什么不呢?

AddressTableViewController 上为您的 InputViewController 创建一个属性,但将其创建为 weak 并且您可以访问它而不会导致任何问题(保留循环)。

例如

@property (weak, nonatomic) InputViewController *parentInputViewcontroller;

然后您应该能够从您的 tableView 子类中访问 self.parentInputViewController 上的任何数据。

关于ios - 通过对象将数据从父 ViewController 传递到子 UITableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30496693/

相关文章:

ios - 为什么 "tableview.cellForRowAtIndexPath(indexPath)"给我一个 BAD_ACCESS 错误?

swift - 包含带有 LLVM 模块映射的 libxml2

ios - IOS 跨应用共享数据

ios - 在没有动画的情况下呈现 UIViewController 会显示演示者 UIViewController 的时间

ios - 仅在调试变体上启用 UIFileSharingEnabled

ios - 设计UISegmentControl

iphone - 在 Swift 中使用枚举和 switch case 组合

swift - ios沙盒恢复非消耗性IAP无需购买

php - APNS PHP 推送通知不工作。超时

ios - 正确定义 UITableViewCell 调整大小的约束