Reputation: 136
I have an in integer array that I'd like to pass from one view controller to another.
I can pass strings from one view to a label in another however I'm not able to do that with an int array.
Basically I have declared the following in viewController_A: -
int totalArray[30];
and I want to pass it to viewController_B
Upvotes: 0
Views: 1145
Reputation: 583
Here, I describe easy way to pass array between two controller.
Suppose you want two pass array from controller1 to controller2.
- create array in controller2.
- import controller2 file in controller1.
- create object of controller2 file.
Code:
controller2 *cn2 = (controller2 *)[self.storyboard instantiateViewControllerWithIdentifier:@"storyboard_identifier"]; // create object of second controller
cn2.array_name = array_name; //pass first controller array to second controller
[self.navigationController pushViewController:cn2 animated:YES]; // redirect to second controller
Thank You.
Upvotes: 0
Reputation: 1410
There shouldn't be any difference between passing an integer array and a string from one ViewController to another.
Sending data between ViewControllers is relatively simple, Here's a link to something I found useful when I first sent data between ViewControllers. Passing Data between View Controllers
Make sure your array declaration is strong:
@property (nonatomic, strong)NSMutableArray *exampleArray;
Edit: Trying to pass an integer array would be a lot more work than holding your values inside an NSMutableArray or NSArray since you cannot declare a c-style array as a property without a work-around as can seen in this post Create an array of integers property in Objective C
If you look at robottobor's answer on that link you will see how you can implement the array as a property, but again I strongly suggest you use an NSMutableArray or NSArray.
Upvotes: 3
Reputation: 16124
Just make a property in the receiving view controller: @property(nonatomic, strong) NSArray *array
Then in prepareForSegue just access that property destViewController.array = intArray
Upvotes: 0