0

I've got one variable and I need this variable during the whole programm. Now code is the next: .h file

extern RequestParams* newUser;

.m file

RequestParams* newUser;

But it works bad. Information doesn't get to this variable. What is the best way to solve this problem? Thank you.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • You need to import the header file (the one that has `extern RequestParams* newUser;`) into every .m file that wishes to access the variable. – Nicolas Miari Aug 07 '14 at 10:49

2 Answers2

2

You will have to use NSUserDefault or Singleton to handle the values of varaiables accross various Controllers.

These both are used in such a scenario where you need to maintain and access the variable values across multiple View Controllers. You can opt for either one based on your choice. NSUserDefault can store multiple key-value pairs that are accessible globally throughout the app. Singleton helps you create a object / variable which is static and hence no other instance of it is created afterwards. Only a single instance is retained throughout the app.

The following links might help you.

Singleton Tutorial

Another Singleton Guide

NSUserDefault Tutorial

Another NSUserDefault Tutorial

Hope this helps !

Community
  • 1
  • 1
Dhrumil
  • 3,221
  • 6
  • 21
  • 34
1

If you required only in you program then you can create it as singleton

//  constants.h

+ (RequestParams*) newUser;

//  constants.m

+ (RequestParams*) newUser{ 

    static RequestParams* instance = nil;

    if (instance == nil) {
        // initiate your instance here;
    }
    return instance;
}

// you can Use it where you required 

[constants newUser];

If you you want to keep to when application is closed then you need to use NSUserDefault to save it.

If this help you then you can accept it as solution.

vnaren001
  • 244
  • 2
  • 11