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

- 398
- 3
- 19
3 Answers
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

- 6,318
- 1
- 26
- 40
-
Thank you very much @Nilesh Patel. I used app delegate :) – Mostafa Fallah May 26 '15 at 08:26
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..

- 4,500
- 3
- 23
- 41
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:

- 1
- 1
-
thank you @Maik639 , i mean define and get global variable.sorry for my mistake – Mostafa Fallah May 26 '15 at 08:15