0

I am copying an array from first viewcontroller to second viewcontroller by adding the code in viewdidappear in firstviewcontroller.

secondViewController *xyzVC = [[ContactViewController alloc]initWithNibName:@"firstvc" bundle:nil];
xyzVC.arr = self.tableData;
NSLog(@"tabledata %@",tableData);
NSLog(@"arrdata %@",xyzVC.arr);

the arr data is getting the data same as tabledata. but when i use it in the secondviewcontroller i am getting null value as arr data.In second viewcontroller i had imported firstviewcontroller.h and written the code in secondviewcontroller.h file

@property (strong, nonatomic)NSMutableArray *arr;

and in .mfile

i am using the arr in the tableview.If i am alloc init the arr i am getting null data,if not then the same result.i need to alloc init or not and if yes the previous data will gets overwrites?I am new to ios programming help me out.

  • Put your above code for transferring data before you push to another view. Not in viewdidload or viewwillappear. – Manthan Aug 26 '14 at 11:34
  • @Manthan can u please explain what is putting the code for transferring data before you push to another view?I am not pushing the second view controller from the first view controller.I dont have any push action to second view controller. – teja nagendra Aug 26 '14 at 11:43
  • Check first NsLog for self.tableData and then alloc and init your array then you will get the data in your array. – Manthan Aug 26 '14 at 11:45
  • What do you get here at NSLog(@"tabledata %@",tableData)??? – Manthan Aug 26 '14 at 11:49
  • The above code should "pass" the array. If you're getting a `(NULL)` on the other side it's because you're using a different copy of your second VC -- doing another alloc/init of the VC elsewhere. If you're getting an empty array `()` it's because you're somehow emptying the array (in the first VC) after passing it. – Hot Licks Aug 26 '14 at 12:38
  • @Manthan tableData nslog is tabledata ( { SearchString = "Elex r"; contact = ""; } – teja nagendra Aug 26 '14 at 13:10
  • @HotLicks Thanks for your reply and Good Catch,Yes i am doing another alloc and init in another view controller which pushes it to the secondVC.The data in the firstVC is not having any push to secondVc.Now what i should do? – teja nagendra Aug 26 '14 at 13:16
  • How you solve the problem is up to you -- it's a simple matter of passing an address from point A to point B. (But for some reason this seems to confound a lot of people.) There is no "cookbook" answer. – Hot Licks Aug 26 '14 at 15:02
  • But look here:http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Hot Licks Aug 26 '14 at 15:53

1 Answers1

1

You need to alloc memory space to array before assigning you can do like bellow:

secondViewController *xyzVC = [[ContactViewController alloc]initWithNibName:@"firstvc" bundle:nil];
xyzVC.arr = [[NSMutableArray alloc] init];
[aMutArrUserData addObjectsFromArray:self.tableData];
NSLog(@"tabledata %@",tableData);
NSLog(@"arrdata %@",xyzVC.arr);
Rajesh
  • 948
  • 7
  • 13
  • How to cal this in the second view controller.What i need to do in viewdidload of secondviewcontroller.As i want to use this arr in table view. – teja nagendra Aug 26 '14 at 11:51
  • You need to only access member variable any where with self.arr in secondViewController. You just need to call [tableview reloadData]; in viewWillAppear – Rajesh Aug 26 '14 at 11:55
  • Thanks for your support Rajesh,i had used with self.arr but i am getting null value when logged the arr.Is there anything to do in viewdidload about this array? – teja nagendra Aug 26 '14 at 12:03
  • I have created sample application and it is working fine at my end. I have use this self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; self.detailViewController.arr = [[NSMutableArray alloc] init]; [self.detailViewController.arr addObject:@"a"]; [self.navigationController pushViewController:self.detailViewController animated:YES]; and in next view - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%@",self.arr); } it will print a in consol – Rajesh Aug 26 '14 at 12:31