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

iOS

Switching which NSArray to generate by selecting a button?

Hi There,

New to this, and I'm trying to expand on the Crystal Ball App to get some more practice in. Basically I want to have three arrays, which I think I have set up after reading around, but I'd like to make an IBAction (I think?) that makes a specific array the only one it generates from. Then if a user selects a different array, it only generates from that etc. I've had a good Google, but I don't think it's a case of there being no information on what I need - it's not being able to efficiently describe what it is that I need!

Best,

Tom

5 Answers

Patrick Cooney
Patrick Cooney
12,216 Points

If I were working on this problem I would probably create a new model (or put this in one of your current models that controls the arrays) with a method that accepts a NSArray as the argument and returns an array. This way you can always work with the "same" array (currentArray in the following example) because the data in it is being switched to whichever array you choose. The following code is by no means the full answer to your question but hopefully it will be a good starting point. Try a few things, if you still can't get it, come on back and we can give you some more help. For some reason trying to add labels is breaking the code blocks. The top block is part of your header and the bottom block is part of your implementation.

 @property (strong, nonatomic) NSArray *currentArray;

- (NSArray *) switchArray:(NSArray *) toNewArray;
- (NSArray *) switchArray:(NSArray *) toNewArray {
   _currentArray = toNewArray;
   return _currentArray;
}

Cheers for that Patrick - will report back on how it goes!

I'm really sorry Patrick, but I couldn't seem to work out how to integrate the code above into my Segmented Control. I've tried running through a lot of tutorials, but I can't seem to combine any to get to the solution. I've attached my code below, basically what I'm trying to do is once certain buttons on the segmented control are pressed, the makePrediction method needs to generate from the array selected by the segmented control. Really stuck on this one!

-(IBAction)switchcontrol:(id)sender; {
if (predControl.selectedSegmentIndex == 0){

        }
if (predControl.selectedSegmentIndex == 1){

}

if (predControl.selectedSegmentIndex == 2){

}

- (void) makePrediction {
NSUInteger index = arc4random_uniform(self.predictionArray.count);
self.predictionLabel.text = [self.predictionArray objectAtIndex:index];

[UIView animateWithDuration:1.0 animations:^{
    self.predictionLabel.alpha = 1.0;
}];
Patrick Cooney
Patrick Cooney
12,216 Points

Is predictionArray a property? If so I would add the code I posted earlier to your viewcontroller.h and .m (normally you shouldn't do this because it combines business logic with your view but you can refactor it into a model later.) then all you need to do is add a line in each of your if blocks self.predictionArray = [self.predictionArray switchArray:<name of your array>];

This should work if I'm not mistaken. Today has been a long day already though so there may be an error in there. I'll double check the logic a little later when my brain starts working again.

Basically you just need to write a method that will change the value of predictionArray within your if blocks. That way you don't need to edit any of the code in your makePrediction method.

Thanks so much for your help Patrick - just added the code to the header and implementation, and also added the above code into the if blocks - however I'm now getting an error saying :

no visible @interface for nsarray declares the selector 'switchArray'
Patrick Cooney
Patrick Cooney
12,216 Points

That's my bad. You're getting that because I had you call our method on an NSArray which doesn't have access to our method. I'll explain it better later but I'm typing from my iPhone right now. Try _predictionArray = [self switchArray:<your array>];

Patrick, thank you so much for this! It's now working like a dream - can't thank you enough!

Patrick Cooney
Patrick Cooney
12,216 Points

No worries. The key is to go through the code I provided and make sure you understand what is happening in each line. Don't focus on the syntax but rather the logic of what's happening. The next step for you would be to go in and separate out your code into a model and try to get this working using the model.