• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 将数据从 ViewController 传递到实例化的自定义 viewcontroller

[复制链接]
菜鸟教程小白 发表于 2022-12-13 06:57:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试将数据从我的数据库传递到在此函数中实例化的新 View Controller 中的 UILabels 中。我已经测试了数据是否被正确抓取,并且工作正常。

我相信我没有正确引用 DataViewController 中的 UILabel 进行实例化。当我尝试 NSLog (dataViewController.cartAddress) 时,我得到空值

有什么帮助吗?

ViewController.m

- (void) mapViewGMSMapView *)mapView didTapInfoWindowOfMarker:    (GMSMarker *)marker {
NSObject_DatabaseHelper *dataToPush = [NSObject_DatabaseHelper getSharedInstance];
NSArray *cartDataToPush = [dataToPush findByName:marker.title];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName"Main" bundle:nil];
DataViewController *dataViewController = (DataViewController *)[storyboard instantiateViewControllerWithIdentifier"dataViewController"];

dataViewController.cartAddress.text = cartDataToPush[0];
dataViewController.thumbsUp.text = cartDataToPush[3];
dataViewController.thumbsDown.text = cartDataToPush[4];
dataViewController.pitaBool.text = cartDataToPush[5];
dataViewController.drinkBool.text = cartDataToPush[6];
dataViewController.greenSauceBool.text = cartDataToPush[7];


[self.navigationController pushViewController:dataViewController animated:YES];



Best Answer-推荐答案


当你实例化 viewController 时,它还没有设置它的 UI。 因此,在 viewController 的 viewDidLoad 回调触发之前,您将无法与 UILabel 进行交互。因此,最好的方法是设置 NSString 属性并在 viewDidLoad 中将它们设置为 UILabel。类似的东西

@property (nonatomic, strong) NSString *cartAddressText;
/* ... */

然后在 viewDidLoad 中

- (void) viewDidLoad {
     [super:viewDidLoad];
     self.cartAddress.text = self.cartAddressText;
     /* ... */
}

关于ios - 将数据从 ViewController 传递到实例化的自定义 viewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30008174/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap