0

I am new in programming and I have problem with moving objects from one VC to another.

My second VC have NSArray * objects. Third VC have NSMutableArray *products. I have modal segue from 2 -> 3 Here is my segue method:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"Products segue"]) {
     ProductsViewController*pvc = segue.destinationViewController;
     pvc.products = [self.objects mutableCopy];
}

}

In ProductsViewController i create few objects:

-(IBAction)addSomeObjects {
products = [NSMutableArray new];
[products addObject:@"Cola"];
[products addObject:@"Pepsi"];
[products addObject:@"Fanta"];
[products addObject:@"Red bull"];
[products addObject:@"Monster"];

If I NSLog my IBAction method products successfully added my objects but when I dissmissViewController my objects array is empty.

Thanks for any help.

gran33
  • 12,421
  • 9
  • 48
  • 76
M.Koptak
  • 63
  • 5
  • 1
    This: `pvc.products = [self.objects mutableCopy]` creates a new mutable array that contains the same objects as `self.objects`. This: `products = [NSMutableArray new]` creates a new empty array. There is nothing in what you've shown that would change `self.objects`. – Phillip Mills Jul 16 '14 at 17:53
  • So what should i do to change my self.objects? – M.Koptak Jul 16 '14 at 17:56
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Hot Licks Jul 16 '14 at 18:25

1 Answers1

1

M. Koptak. Welcome to programming.

In this example, I would suggest you create a model to manage products. A model is a class that represents data. In this example, create a class to represent the collection of products. For this I will call it a catalog.

// In Catalog.h
@interface Catalog : NSObject
@property (nonatomic, readonly) NSMutableArray *products;
@end

// In Catalog.m
@implementation Catalog
- (id)init
{
    self = [super init];
    if (self) {
        _products = [NSMutableArray array];
    }
}
@end

Now that I have a class which can manage products, I need to have catalog properties (of type Catalog) in both the first view controller and ProductsViewController.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Products segue"]) {
        ProductsViewController *pvc = segue.destinationViewController;
        pvc.catalog = self.catalog; // Pass the catalog between the view controllers.
    }
}

- (void)viewDidLoad
{
    …
    if (self.catalog == nil) // Make sure an instance of `Catalog` has been created!
        self.catalog = [[Catalog alloc] init];
    …
}

Finally, in ProductsViewController

-  (IBAction)addSomeObjects
{
    [self.catalog.products addObject:@"Cola"];
    [self.catalog.products addObject:@"Pepsi"];
    [self.catalog.products addObject:@"Fanta"];
    [self.catalog.products addObject:@"Red bull"];
    [self.catalog.products addObject:@"Monster"];
}

Now when you dismiss ProductsViewController, the first view controller will have all the new products.

Note: This is the very first step in how to share data. It skips proper class names, data protection, and data validation, but it gets you started.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • Thanks for show me better solution. It works fine, but i need that objects in my NSArray :) It work with my search bar so i need fill it with that objects to show that objects in my search bar table view. – M.Koptak Jul 16 '14 at 18:44
  • Oh i got it. In viewWillAppear i've put self.objects = self.catalog.products; Now it works finally ! :) many thanks to you – M.Koptak Jul 16 '14 at 18:59