global variable read/write access
I need 开发者_如何学Pythonto access (read/write) some variables in all of my views.
The variables will be defined in view 1, then used and set to a new value in view 2.
How can i do this?
Ya , there is much a easy way to handle this.....
You can take a Global Variable
In your Delegate.h file declare your variable:
@interface Smoke_ApplicationAppDelegate : NSObject {
UIWindow *window;
UINavigationController *navigationController;
NSString *messageString; //This would be your String Variable
} @property(nonatomic,retain)NSString *messageString;
Secondly in Delegate.m file
@implementation Smoke_ApplicationAppDelegate
@synthesize window; @synthesize navigationController; @synthesize messageString; //
Synthesize it over here..
This is Done .Now you can use this String Variable in All/any class you want..
To use this Global Variable.
Just import you Delegate file make the obj of it....
import "DelegateFile.h"
@implementation About
DelegateFile *appDel;
Now in Your class.m
-(void)viewDidLoad { [super viewDidLoad];
appDel=[[UIApplication sharedApplication]delegate];
}
Now you can access it anywhere in your class by this Object:
appDel.messageString
Just follow my Steps Carefully After giving so much pain to my finger, I am sure this is definitely going to help you.....
Have a easy life,
Please don't declare them as instance variables on your application delegate, it's not really good design practice for MVC.
Your best option is to create a shared data model. This can be what is called a 'singleton' class so there is only every 1 instance of it. This model class can then store any central information that is needed by various views.
Rob Napier's answer over here is a good reference as are other links from that page. In particular I'd commend Mike Ash's post here.
'Singletons' are great. They encapsulate the data in a model class where it belongs and avoid creeping global variables which make code less easy to decouple in future.
精彩评论