使图像可以从其他ViewController访问

我只想知道制作一个名为profpic的图像的代码,可以访问我制作或打算制作的所有其他ViewController。 我已经阅读了许多关于全局variables,公共variables和其他build议的文章。 如果有帮助,我特别使用这个来显示ViewControllerA的图像作为ViewControllerB的背景。

你可以使用单例类,并把UIImage放在上面。 将其设置在ViewControllerA并在ViewControllerA获取

 @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 

访问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;