0

I have 5 viewcontrollers lets say A,B,C,D and E and all these viewcontrollers will be pushed to navigation controller as A->B->C->D->E.

I have an array in A and I need to pass this to array E. In A, I should not create object for E and vice versa.

What are the approaches to pass data between viewcontrollers as per my requirement?

satyanarayana
  • 265
  • 4
  • 14
  • you can also declare NSMutableArray in Application delegate and assign objects to this array in A view controller and you can automatically get this array in view controller E. – Pradhyuman sinh Oct 26 '13 at 05:18

3 Answers3

1

You can use Notification center method. In your view controller's viewdidload method write following code..

[[NSNotificationCenter defaultCenter] addObserver: self
                                     selector: @selector(anymethod:) 
                                         name: anyname 
                                       object: nil];

and method..

- (void)anymethod:(NSNotification *)notification 
{
  NSLog(@"%@", notification.userInfo);  
}

and from other view controller pass data like,

[[NSNotificationCenter defaultCenter] postNotificationName:@"anyname" object:self userInfo:anydata];
iPatel
  • 46,010
  • 16
  • 115
  • 137
Ashish P.
  • 848
  • 5
  • 12
1

(1)You can use NSNotification:

An NSNotification has a property called userInfo that is an NSDictionary. The object is the NSObject that is posting the NSNotification. So usually I use self for the object when I'm setting up the NSNotification because self is the NSObject sending the NSNotification. If you wish to pass an NSArray using an NSNotification I would do the following:

NSArray *myArray = ....;
NSDictionary *theInfo = [NSDictionary dictionaryWithObjectsAndKeys:myArray,@"myArray", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self userInfo:theInfo];

And then catch it using the following:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doTheReload:) name:@"reloadData" object:sendingObject];

where sendingObject is the object that is sending the NSNotification.

Finally, decode the array in doTheReload: using:

 NSArray  *theArray = [[notification userInfo] objectForKey:@"myArray"];

That always works for me. Good luck!

(2) ApplicationDelegate:

you can also declare NSMutableArray in Application delegate and assign objects to this array in A view controller and you can automatically get this array in view controller E.

Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
-1

Many recommends AppDelegate and sure it does work because AppDelegate itself is a singleton, but, you know, you should not put a piece of data into a class that it does not belong to (that's what people call object oriented programming). Anyway, it does work, and probably be fine if you want to save a bit of time, a bit of trouble creating a new class, and are happy with violating some old object oriented principles.

Notification Center should be used for notifications only: some event happens in one place, and another object wants to get notified, maybe along with some data regarding that event. Not the best for pure data sharing. Performance is not a problem since it nails down to function calls (assuming you're passing pointers only, not copying some big data).

IMHO, you have two options (at least):

Create a singleton class dedicated to contain the data. Lot of resources tell you how to do this, but basically a singleton class in Objective-C looks like this

@interface S 

+(S*)singleton;

@end

@implementation S
+(S*)singleton { // iOS sometimes use 'sharedInstance' instead of 'singleton'
    static S* o = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        o = [[self alloc] init];
    });

    return o;
}

@end

And whenever you need to access it

[S singleton] ...

The second option is applicable when there is only one instance of A in the whole application life time (this happens very often if A is a root view controller). In this case, you can just turn A into a singleton. The code in your app delegate will look like this

A* a = [A singleton];

UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:a];

And E can just access all A's data with [A singleton]

Khanh Nguyen
  • 11,112
  • 10
  • 52
  • 65