0

I am new to objective-C and working on an app in which there are multiple viewController Files. I need to access Value of variables set in one file to be accessible in other files. How can i implement this.

What i was doing that i created a class Globals.m and declared variables in it.

#import <Foundation/Foundation.h>

@interface Globals : NSObject

@property  NSString*  firstName;
@property NSString* lastName;
@property NSString* emailId;

@end

My question are: 1.will the above Declaration make these variables retain there values in different files?

  1. Where should i crete a object of this class which should be accessible in all files.
7
  • You can remove the @property annotation... (It's just for generating getter/setter methods.) Then, use #import to import this file wherever you want to use the variables. Commented Apr 2, 2015 at 7:05
  • after removing @property it is giving error cannot declare variable inside interface Commented Apr 2, 2015 at 7:07
  • 1
    I'd recommend having a look at a singleton pattern instead Commented Apr 2, 2015 at 7:08
  • Hmm, you know what? Just get rid of @interface (and @end). There is no need for annotations in this file because you're just declaring some constants. Commented Apr 2, 2015 at 7:09
  • 1
    If you want an object (my answer does not create a class... just a flat file), then Popeye's answer is better. Commented Apr 2, 2015 at 7:16

1 Answer 1

2

I'd maybe suggest using a Singleton pattern as it will allow you to hold an instance of the class through your app. So you could set firstName in one class and get it in another.

MySingleton.h

@interface MySingleton : NSObject

// Our properties we want to set.
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString *emailName;
// etc...

// Our class method for getting the shared instance.
+ (MySingleton *)sharedInstance;

@end

MySingleton.m

#import "MySingleton.h"

@implementation MySingleton

+ (MySingleton *)sharedInstance
{
    static dispatch_once_t pred = 0;
    static id sharedObject = nil;
    dispatch_once(&pred, ^{
        sharedObject = [[self alloc] init];
    });

    return sharedObject;
}

@end

Then in your other classes all you need to do is import the MySingleton class and do something like :

MySingleton *singleton = [MySingleton sharedInstance];
[singleton setFirstName:@"Bob"];

For more information on Singleton Patterns and other Design Patterns here is a tutorial that you could read through iOS Design Patterns. Also a good read is Correct Singleton Pattern Objective C (iOS)?

Sign up to request clarification or add additional context in comments.

7 Comments

After this how can i access sharedObject in other files
See the edit I have just done. All you need to do is call the class method something like MySingleton *singleton = [MySingleton sharedInstance]; and then you can access all the properties it has.
variables when accessed in different in different files will retain there value??
@Popeye you still need #import, no? (Assuming your classes are in different files...)
@martinjakubik Yes sorry I'll update with that but I normally assume imports are a given.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.