Reputation: 632
I´m lost on this one, already read some things all over the internet but my problem is that i need to understand how to properly use the singleton. My problem is, at some point in my app i do what is below:
myVariable = [NSEntityDescription insertNewObjectForEntityForName:@"Entity"
inManagedObjectContext:context];
I need to preserve myVariable
and use it in other views, and i read somewhere that this is the best way if i want to use a variable through all my views. I have followed this example but i really don´t know how to use it, can someone explain it to me?:
@interface DataLoader : NSObject {
NSString *someProperty;
//(i think i need myVariable here, and not type NSString)
}
@property (nonatomic, retain) NSString *someProperty;
+ (id)sharedManager;
@end
@implementation DataLoader
+(id)sharedInstance {
static dispatch_once_t p=0;
__strong static id _sharedObject = nil;
dispatch_once(&p, ^{
_sharedObject = [[self alloc]init];
});
return _sharedObject;
}
@end
How can i set myVariable and then in another views use it?
Regards
Upvotes: 0
Views: 56
Reputation: 80265
The usual way is to have the controllers pass the variable on to the next one whenever they are pushed on the navigation stack, e.g. in prepareForSegue:
. Just give your view controllers a strong @property
to keep track of it.
SomeViewController *nextVC = segue.destinationController;
nextVC.myVariable = self.myVariable;
That is how it is done in many instances of Apple's sample code with managed object context, it certainly is good pattern.
Upvotes: 1