使图像可从其他 ViewController 访问
Posted
技术标签:
【中文标题】使图像可从其他 ViewController 访问【英文标题】:Make image accessible from other ViewController 【发布时间】:2015-06-21 00:06:44 【问题描述】:我只想知道让我制作或打算制作的所有其他 ViewController 可以访问名为 profpic 的图像的代码。我已经阅读了许多关于全局变量、公共变量和其他尚未起作用的建议的帖子。如果有帮助,我专门使用它来显示来自 ViewControllerA 的图像作为 ViewControllerB 的背景。
【问题讨论】:
Passing data between view controllers 【参考方案1】:您可以使用单例类并将UIImage
放在上面。设置在ViewControllerA
,获取到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
访问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;
【讨论】:
这不是单例,它只是一个提供对共享对象的访问的类。单例是只能实例化一次的类。 我仍然很困惑,为什么这如此困难。这应该很简单,哈哈,我希望我知道怎么做!哈哈以上是关于使图像可从其他 ViewController 访问的主要内容,如果未能解决你的问题,请参考以下文章
将所选图像从 uiscrollView 传递到其他 viewController
一些目录可从公共 URL 访问。如何在 yii 中避免这种情况
从Swift中的另一个ViewController访问rightBarButton函数