tags:

views:

339

answers:

2

Hi friends,

      Provide some tips to get rid of the following scenario.

description:

I had two viewControllers namely ViewController1 and ViewController2,so obiviously we have ViewController1.h,ViewController1.m and ViewController2.h,ViewController2.m. Now i declared an NSString *string1; in ViewController1.h and declared as a property as @property(nonatomic,retain)NSString *string1; and synthesized in ViewController1.m as @synthesize string1;

and in ViewController1.m, i set the string1 value as string1=@"Hello Every One";

Now similarly i declared an NSString *string2; in ViewController2.h and declared as a property as @property(nonatomic,retain)NSString *string2; and synthesized in ViewController2.m as @synthesize string2;

if i want set the string1 value( in ViewController1.m) to string2 (in ViewController2.m) ,How can i do that.

A: 

You can use a model object that contains the two strings and that is known by both controllers.

If you want each controller to be notified whenever the other update the value of the strings, you can use notification mechanism. This allows your model to let other objects know about its changes and stay independant from these objects.

mouviciel
+2  A: 

That depends on where the code you want to set string1 in is being run. If it is in some outside class with access to both view controller objects, then it's simple. If you have ViewController1 object vc1, and ViewController2 object vc2, then all you do is:

[vc1 setString1:[vc2 string2]];

If you want to set string1 from code run within ViewController2, then you use the notification mechanism. In ViewController1's initialization routine, you put:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aChangeStringMethod:) name:@"anyStringJustMakeItUnique" object:nil];

And define:

-(void)aChangeStringMethod:(NSNotification)notification{
     string1 = [((ViewController2 *)[notification object]) string2];
}

Then, in ViewController2, when you want to change the string:

[[NSNotificationCenter defaultCenter] postNotificationName:@"anyStringJustMakeItUnique" withObject:self];

The same technique is used when you are changing the string from some third class that has access to vc2 but not vc1. ViewController1 code is the same as above, and when you want to change the string:

[[NSNotificationCenter defaultCenter] postNotificationName:@"anyStringJustMakeItUnique" withObject:vc2];

The trickiest part is if you want to change the string from within ViewController1 (assuming that you don't have access to the object vc2). You have to use two notifications: the one above, and also, for ViewController2:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(launchTheOtherNotificationMethod:) name:@"anotherNotificationName" object:nil];

-(void)launchTheOtherNotificationMethod:(NSNotification)notification{
     [[NSNotificationCenter defaultCenter] postNotificationName:@"anyStringJustMakeItUnique" withObject:self];
}

Then, when you want to change the string:

[[NSNotificationCenter defaultCenter] postNotificationName:@"anotherNotificationName" withObject:nil];

If you think this is too complex or causes too much overhead, the simpler solution is to have, as fields in ViewController1 and ViewController2, pointers to one another. Then, within ViewController1:

string1 = [myVC2 string2];

And if you make these fields properties, then, from the outside:

[vc1 setString1:[[vc1 myVC2] string2]];

And even:

[[vc2 myVC1] setString1:[vc2 string2]];
executor21