I just want to know the code for making an image called profpic accessible to all other ViewControllers that I make or intend to make. I have read many posts on global variables, public variables, and other suggestions that have yet to work. If it helps, I am specifically using this to display an image from ViewControllerA as the background for ViewControllerB.
            Asked
            
        
        
            Active
            
        
            Viewed 68 times
        
    1
            
            
         
    
    
        rmaddy
        
- 314,917
- 42
- 532
- 579
 
    
    
        Will Von Ullrich
        
- 2,129
- 2
- 15
- 42
- 
                    [Passing data between view controllers](http://stackoverflow.com/q/5210535/643383) – Caleb Jun 21 '15 at 02:37
1 Answers
0
            
            
        You can use a singleton class and put the UIImage on it. Set it in ViewControllerA and get it in ViewControllerB
@interface MySingleton : NSObject 
@property (nonatomic, strong) UIImage *myImage;
+ (MySingleton *)sharedInstance;
@end
@implementation MySingleton
#pragma mark Singleton Methods
+ (MySingleton *)sharedInstance {
    static MySingleton *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
return sharedInstance;
}
- (id)init {
  if (self = [super init]) {
  }
  return self;
}
@end
To access myImage
// set myImage in ViewControllerA
MySingleton *mySingleton = [MySingleton sharedInstance];
mySingleton.myImage = [UIImage imageNamed:@"imageName"];
// get my image in ViewControllerB
MySingleton *mySingleton = [MySingleton sharedInstance];
myImageView.image = mySingleton.myImage;
 
    
    
        Mahmoud Adam
        
- 5,772
- 5
- 41
- 62
- 
                    1**That's not a singleton,** it's just a class that provides access to a shared object. A singleton is a class that can only be instantiated once. – Caleb Jun 21 '15 at 00:54
- 
                    im just still so confused why this is so difficult. this should be so simple lol i wish i knew how!! haha – Will Von Ullrich Jun 21 '15 at 02:48