User18474728
User18474728

Reputation: 361

Passing data with delegate but still nil why?

I made 2 viewcontrollers and implemented on tabbar controller. I passed some data from A vc to B vc with using delegate. When I checked the log it showed me correct value. But when I moved to B vc the value I passed was nil. (the value is for tableview.) Here is my code.

in A vc

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

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

in B vc

-(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];
}

Upvotes: 0

Views: 47

Answers (2)

Danny Lau
Danny Lau

Reputation: 157

Please check the way you move to B-VC and whether the instance of (B-VC) you used in the A -VC is same to self in the B-VC “viewWillAppear” method. It seems when u move to B-VC, A new instance has been created.

Or you can implement the 'setDownloadArr' method, log the value and show when the value become nil.

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

To have the array printed inside viewWillAppear without nil

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

you need to give it a value before you show bVC from aVC , whatever you use present/segue/push , also don't forget to declare it as strong , you need to do this

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

Upvotes: 2

Related Questions