How to Access Objects from one ViewController to another ViewController

How to Access Objects from one ViewController to another ViewController

Provide some tips to get rid of the following scenario.


I have 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 it as a property

@property(nonatomic,retain) NSString *string1; 

and synthesized it in ViewController1.m as

@synthesize string1; 

and in ViewController1.m, i set the string1 value as

string1=@"Hello Every One"; 

Similarly I declared

NSString *string2; 

in ViewController2.h and declared it as a property

@property(nonatomic,retain)NSString *string2; 

and synthesized it 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?

How do I compare objects in Objective-C?


Where is the difference between writing 0.1 and 0.1f in objective-c?
That depends on where the code you want to set string1 in is being run.

Why can't I @synthesize accessors in a category?
If it is in some outside class with access to both view controller objects, then it's simple.

How to dynamically subclass a class and instantate the subclass in Objective C?
If you have ViewController1 object vc1, and ViewController2 object vc2, then all you do is:.
iPhone's UITextView - how do you know when a text you append is out of view
[vc1 setString1:[vc2 string2]]; 
If you want to set string1 from code run within ViewController2, then you use the notification mechanism.

What can I do if two methods call each other and I don't want to make one of them public in the header file?
In ViewController1's initialization routine, you put:.
How do I create a file that only contains functions in objective-c?
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aChangeStringMethod:) name:@"anyStringJustMakeItUnique" object:nil]; 
And define:.
Ball to Ball Collision - Gains significant velocity upon collision
-(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]]; 


viewControllers are a stack, so whichever one got called last is on top of the stack, and the one it got called from is its parent.

So assuming viewController1 gets called first, and then viewController2 gets called from within that, then all you have to within viewController2.m is:.
[[self parentViewController] setString1:string2] 


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..

88 out of 100 based on 68 user ratings 718 reviews