I've been trying to bring an array from one view controller to another but for some reason when I pass it and try to use it, it has the value nil
. I've seen many ways to do it here but none of them worked for me. I'm using a UINavigationController
and changing view controllers with push segues if that is any help, and I'm creating an mutable array in one view controller and trying to read it in the next view controller. I would put my code but I'd rather start fresh since I have no idea what is right about my code anymore for trying to change it with all the solutions I saw here.
Asked
Active
Viewed 1,387 times
-2
-
3You should show at least one of the ways you tried. – rdelmar Dec 02 '13 at 03:28
-
This question is asked daily at certain times of the year. Please research better before asking. – Hot Licks Dec 02 '13 at 03:52
-
like I said I've done research but none of the ways i found worked – DanielMed Dec 02 '13 at 06:12
1 Answers
0
Zaph is definitely right, and creating a Data Model class is definitely the MVC-way to go. However, if this array contains no essential information, and will really only be used in these two classes, the appropriate way to go is:
1 - Save the array in VC1 (in this example named originalArray
)
2 - Make a NSMutableArray property in VC2 (in this example named arrayToPass
)
3 - in prepareForSegue between VC1 and VC2 do the follwing:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"myIdentifier"]) {
MySecondViewController *secondVC = (MySecondViewController* ) segue.destinationViewController;
secondVC.arrayToPass = [NSMutableArray arrayWithArray:self.originalArray]; // it is a good practice to initiate the property first
// or
// secondVC.arrayToPass = [self.originalArray mutableCopy];
}
}
-
I did it the first way and it now gives me '0 objects' instead of 'nil' – DanielMed Dec 02 '13 at 06:23
-
0 objects is already way better than nil :).. Do check that the originalArray has more than 0 objects, and that both of them are declared as strong. – Cutetare Dec 02 '13 at 06:24
-
Yes haha it is way better! Yes the original array has more than 0 objects and yes they are both strong but still get 0 objects :/ – DanielMed Dec 02 '13 at 06:54
-
then try `secondVC.arrayToPass = self.originalArray;`, just keep in mind that by doing this you are passing the array by reference and not by content! – Cutetare Dec 02 '13 at 07:13
-
I disagree, a controller should not contain the model data even if no other class used the data. Embedding the data in the controller class is also a violation of the Single Responsibility principle. – zaph Dec 02 '13 at 12:02
-
`secondVC.arrayToPass = self.originalArray;` gave me `nil` again :/ what do you suggest Zaph? – DanielMed Dec 02 '13 at 17:55
-
got it working with this `-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if([segue.identifier isEqualToString:@"showDetailSegue"]){ UINavigationController *navController = (UINavigationController *)segue.destinationViewController; ViewControllerB *controller = (ViewControllerB *)navController.topViewController; controller.isSomethingEnabled = YES; } }` since im using navigation controller but how would i send an array form a `UIViewController` that is not continuous to the one I want to use it in? – DanielMed Dec 02 '13 at 18:18
-
@DanielMed, like I mentioned above in the answer. You simply cast `segue.destinationViewController` to whatever the receiving view controller is (In your case ViewControllerB) – Cutetare Dec 03 '13 at 01:18
-
-
@Joshua can i able to do with out the prepareForSegue to pass the array to the next view controller. – Hari Narayanan Sep 22 '16 at 15:09
-