0

I am developing a project that has some views. I want to set a value in first view as public variable and using this value on other forms.
For example user will set a value in a textfield in first view and then I will make a query base on this field on other views.
Thanks a lot

Mostafa Fallah
  • 398
  • 3
  • 19

3 Answers3

1

You can use Delegate class (Its singleton class) to store the required data & then you can access it anywhere in your application.

OR

Create your own singleton class to manage it. Reference link to create custom singleton class

Nilesh Patel
  • 6,318
  • 1
  • 26
  • 40
1

It's funny i'm working with this last month.. haha.. Here is what i'm using right now, this suits their purpose for me..

//PublicVariables.h
@interface PublicVariables : NSObject

@property (nonatomic) NSString *variableOne;
@property (nonatomic) NSString *variableTwo;

+ (PublicVariables *)sharedVariables;

@end

//PublicVariables.m
@implementation PublicVariables

static id _instance = nil;

+ (PublicVariables *)sharedVariables
{
    if (!_instance)
        _instance = [[super  allocWithZone:nil] init];

    return _instance;
}

// optional
- (void)setVariableOne:(NSString *)value
{
    self.variableOne = value;
}

@end

Using it will be something like this: NSString *varOne = [PublicVariables sharedVariables].variableOne;

i suggest you import that PublicVariables.h inside [ProjectName]-Prefix.pch file.. Good luck to you.. Happy coding..

0yeoj
  • 4,500
  • 3
  • 23
  • 41
0

You should do this with the prepareForSegue-method of UIViewController

More information here : Passing Data between View Controllers and https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/prepareForSegue:sender:

Community
  • 1
  • 1