58669

global variable read/write access

Question:

I need to 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?

Answer1:

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,

Answer2:

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 <a href="https://stackoverflow.com/questions/2410830/apple-singleton-example-query" rel="nofollow">over here</a> is a good reference as are other links from that page. In particular I'd commend <a href="http://www.mikeash.com/pyblog/friday-qa-2009-10-02-care-and-feeding-of-singletons.html" rel="nofollow">Mike Ash's post here</a>.

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

Recommend

  • best practice for how to inherit property from parent class and override setter and getter
  • objective C: use NSMutableArray in different classes
  • How do I convert RGB into HSV in Cocoa Touch?
  • _iVar vs. iVar_ for variable naming [closed]
  • How do I know if text exceeds the bounds of a text view?
  • Adding child progress for multiple file download
  • playing video in iOS 5
  • pyspark, importing schema through json file
  • android: disable opengl ES context switch upon device rotation
  • How can I figure out what UITableViewCell I'm clicking on during a search?
  • Encoding issue with ASIHttpRequest
  • Change color of row programmatically in WatchKit
  • Unload image of UIImageView thats offscreen
  • iOS - MKOverlayView custom view rect fills works, but line draws do not
  • Using extern @class in order to add a category?
  • Specifying virtual keyboard type for EditText in XML
  • Where these are stored?
  • What's the syntax to inherit documentation from another indexer?
  • Creating PDF from TIFF image using iText
  • abstracting over a collection
  • Can't remove headers after they are sent
  • Scipy Leastsq Optional Output Variable (Mesg)
  • C++ pointer value changes with static_cast
  • Scala multiline string placeholder
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • Not able to aggregate on nested fields in elasticsearch
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • how to save the state in userdefaults of accessory checkmark-iphone
  • Assign variable to the value in HTML
  • Converting a WriteableBitmap image ToArray in UWP
  • Database structure design with variable amounts of fields
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • Updating server-side rendering client-side
  • htaccess rewriting URLs with multiple forward slashes
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • How to format a variable of double type
  • Checking variable from a different class in C#
  • How to Embed XSL into XML