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.

9
  • Put your above code for transferring data before you push to another view. Not in viewdidload or viewwillappear. Commented Aug 26, 2014 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. Commented Aug 26, 2014 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. Commented Aug 26, 2014 at 11:45
  • What do you get here at NSLog(@"tabledata %@",tableData)??? Commented Aug 26, 2014 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. Commented Aug 26, 2014 at 12:38

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

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.
You need to only access member variable any where with self.arr in secondViewController. You just need to call [tableview reloadData]; in viewWillAppear
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?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.