gpt4 book ai didi

ios - 用委托(delegate)传递数据但仍然没有为什么?

转载 作者:行者123 更新时间:2023-11-28 20:56:02 24 4
gpt4 key购买 nike

我制作了 2 个 View Controller 并在标签栏 Controller 上实现。我使用委托(delegate)将一些数据从 A vc 传递到 B vc。当我检查日志时,它显示了正确的值。但是当我搬到 B vc 时,我传递的值是零。 (该值适用于 tableview。)这是我的代码。

在 A VC 中

-(void)passData {            
NSMutableDictionary *infoDic = [[NSMutableDictionary alloc] init];
[infoDic setObject:url forKey:@"file_url"];
[downloadArr addObject:fileInfoDic];

Bvc getDataFromA:downloadArr];
[Bvc reloadTableView];
}

在 B 中

-(void)getDataFromA:(NSMutableArray *) downloadArr{
self.downloadArr = [downloadArr mutableCopy];
NSLog(@"my download list%@", self.downloadArr); // This time was ok.
}

- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"Array status %@", self.downloadArr);//This time it showed me nil
[self.tableView reloadData];
}

最佳答案

要在 viewWillAppear 中打印数组而不用 nil

NSLog(@"Array status %@", self.downloadArr);//This time it showed me nil

你需要在从 aVC 显示 bVC 之前给它一个值,无论你使用 present/segue/push ,也不要忘记将它声明为 strong ,你需要这样做

bvc = [[self.tabBarController viewControllers] objectAtIndex:1]; 
[bvc loadViewIfNeeded];
[bvc getDataFromA:downloadArr];

关于ios - 用委托(delegate)传递数据但仍然没有为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52346205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com