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

标签 ios objective-c delegates

我制作了 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/

相关文章:

iOS 本地通知 : app is not asking for permissions to notify the user

C# 委托(delegate),引用解析时间

c# - 具有不同参数数量的方法的委托(delegate)

ios - 如何将WebRTC导入到xcode项目中?

ios 6 下拉刷新

ios - 容器 View 转至相同大小的 View Controller

ios - Swift:如何知道哪个 Storyboard 处于事件状态

objective-c - 替换 nsmutablearray 中的对象

ios - 在相机 OverlayView 中绘制图像

ios - 仅在 iPhone 4S 上对委托(delegate)进行错误访问