开发者

how to pass text between views

开发者 https://www.devze.com 2023-01-08 12:22 出处:网络
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....

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 tex开发者_运维问答t


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


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.


Look into the Singleton pattern.

What should my Objective-C singleton look like?

Then you could do something like:

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

and

//view2
#import "SingletonClass.h"
...
lbl.text = [SingletonClass sharedInstance].savedText;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号