Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Stephen Hancocks
9,271 PointsUse a NSMutableArray in a different class
Hi
Trying to develop my first app. I have used Hpple to parse data from the internet and successfully presented the NSMutableArray in subtitled table view. I know that the array forms with all of the data I want, however I don't want to display it in a table.
I want to form the array in the first screen and then handle the items with the array in subsequent view controllers. What is the best way to reference the initial array?
My only other programming 'experience' is with Visual Basic so I may be approaching my issue incorrectly in the first place.
Any help would be appreciated.
Steve
3 Answers
Sabine Geithner
3,264 PointsFirst of all, try not to "surface" NSMutableArrays in your interface. You should always only use NSArrays there. You can later create a new NSMutableArray if you need to manipulate the data with the copy method. To convert an NSMutableArray to an NSArray just use the arrayWithArray: class method
You can either create a new init method passing the array along or you use the prepareForSegue method setting the array as a property of the destination view controller
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
vc.yourArray = self.yourArray;
}
}
eirikvaa
18,015 PointsI removed my answer, just try out the first one. It's good.
Sabine Geithner
3,264 PointsIt really depends on what you want to do with that array. Do you want to display only one element and that's it? --> set that element as the property of your destination VC with [yourArray objectAtIndex:indexOfYourElement] Do you want to display the elements in consecutive viewcontrollers? --> then it would make sense to pass the array Do you want to store the data in the array to use the app offline --> store your data in coreData and fetch it with fetchControllers when needed
And yes, you have to import the header files in order to create an instance of your destination viewcontroller.
Stephen Hancocks
9,271 PointsThanks again for the reply. I'm currently at work so I will try again tonight.
The array will have 20 elements [the last 18 months accounts and the last 2 years to date figures]. The array will then be used in 5 or more view controllers which will analyse different aspects [last 2 months, last 6 months, last 12 months, year on year results]. It makes sense to me to have the array formed once on the first view controller and then made available to every subsequent view controller.
The sample code forms the array and then uses it in a tableview and all works fine. However it declares *myArray first and then uses myArray and even !myArray. What do the '' and '!' in front of the array do.
Thanks
Stephen Hancocks
9,271 PointsStephen Hancocks
9,271 PointsThanks for the answer. I will try out what you suggest and let you know how it goes.
I'll take on board the NSMutableArray vs NSArray too.
Thanks
Stephen Hancocks
9,271 PointsStephen Hancocks
9,271 PointsI'm still struggling. It must be a simple beginners error. Here is some code I'm using. I have distibutorsNameArray constructed in FirstViewController and I want to use it in SecondViewController. It's formed as follows [showing part of the code from Hpple].
Apologises for what seem like 'childish' code but the 'class' and 'instance' identifier are helping me learn.
I then added the code you provided as follows in the hope it went to the SecondViewController
I tried NSLog on the array from the SecondViewController and it returned (null).
Do I need to import the header files from each controller for it to work? Also does it need to be a function that returns an array as the Hpple sample code I am successfully using to parse the HTML code is a '(void)' function.
Apologises for having to ask again. It's very frustrating as I'm sure the answer is very straightforward.