0

I have one NSMutableArray in FirstViewController declared as firstArray. I want to copy the secondArray into firstArray.

In the SecondViewController,

Self.FirstViewController.firstArray = self.secondArray;

When I attempt to NSLog the firstArray.count from the FirstViewController, it display 0. It should have two objects in the array

Anyone can advise on this?

Igor Borisenko
  • 3,806
  • 3
  • 34
  • 49
  • Post the code please, it seems that secondArray is empty. Anyway i suggest you to use "arrayWithArray" if you want to copy, what you did isn't a copy – Marco Pace May 21 '13 at 13:11
  • http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers/9736559#9736559 – Ankur Arya May 21 '13 at 13:18

3 Answers3

2

You can choose one of this solutions:

  1. Singleton
  2. Passing Data between ViewControllers
  3. Delegation

You can find all the info you need right here: https://stackoverflow.com/a/9736559/1578927

Singleton example:

static MySingleton *sharedSingleton;

    + (void)initialize
    {
        static BOOL initialized = NO;
        if(!initialized)
        {
            initialized = YES;
            sharedSingleton = [[MySingleton alloc] init];
        }
    }
Community
  • 1
  • 1
Segev
  • 19,035
  • 12
  • 80
  • 152
0

It looks like either the second array has already been deallocated when passing the reference to the first view controller, or the first view controller itself has already been nilled out. If the first is true, then you may need a different model object to hold your data rather than persisting it in the controller layer of your app. If that is not the case, then you may want to consider a direct copy. The easiest way of doing this is to declare the firstArray property as the keyword copy rather than strong in your interface file.

If you do need to persist the data in the model layer of your app, a singleton pattern object would indeed be one way of achieving this as EXEC_BAD_ACCESS (nice name!) points out. A slightly more modern (though functionally equivalent) way of writing a singleton is as follows.

@interface MySingleton : NSObject

@property (strong, readwrite) id myData;

 + (id)sharedSingleton

@end

@implementation MySingleton

+ (id)sharedSingleton
{
  static MySingleton *singleton = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    singleton = [[MySingleton alloc] init];
    // Do other setup code here.
  });
  return singleton;
}

@end

Note the use of dispatch_once - this makes certain that the static singleton can only be created once (whereas technically, you can invoke +[NSObject initialize] as many times as you feel like manually, though I'd never advise doing so).

David Doyle
  • 1,716
  • 10
  • 23
0

You may also take advantage of NSNotificationCenter

SecondViewController.m

 [[NSNotificationCenter defaultCenter] postNotificationName:@"arrayFromSecondVC" object:secondArray];

FirstViewController.m

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(populateArray:) name:@"arrayFromSecondVC" object:nil];

}

-(void)populateArray:(NSNotification *)notif
{


 self.firstArray = [notif object];

}

And remove the notification when the viewUnload or didRecieveMemoryWarning method.

Hope it helps.

Regexident
  • 29,441
  • 10
  • 93
  • 100
Harish
  • 1,469
  • 16
  • 43