views:

59

answers:

3

i made 2 views and i want to send text of label on main view to sub view to an want to print it there on another label's text value.... how to pass that text

A: 

If class A handle your view1 and Class B handle view2 then define a interfaces in class B to accept new label to your one of the UI element then call that interface from class A.

Girish Kolari
A: 

Look into the Singleton pattern.

http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like/145403#145403

Then you could do something like:

//view1 
#import "SingletonClass.h"
...
[SingletonClass sharedInstance].savedText = @"blah";

and

//view2
#import "SingletonClass.h"
...
lbl.text = [SingletonClass sharedInstance].savedText;
Kurbz
+2  A: 

I wouldn't use a singleton pattern or any other 'global variable'. This will make your view controllers very tightly coupled and restricts reusability. I would just create a instance variable in the second view controller and set this in the main one before presenting the view.

The second view controller then sets the label.text to the instance variable in (for example) viewDidLoad.

This way, the second view controller doesn't depend on any 'globals' or includes and will be more reusable.

//SecondViewController.h
@interface SecondViewController : UIViewController {
    NSString *theLabel;
}

@property(nonatomic, copy) NSString *theLabel; //Synthesize in implementation

@end

Then in the main view controller:

//Create instance of secondViewController
instanceOfSecondViewController.theLabel = @"Nut";
//Present the second view here
Rengers
Whenever I see someone using global variables or singletons where one isn't needed, I see someone who doesn't know what they're doing.
lucius