使图像可从其他 ViewController 访问 [英] Make image accessible from other ViewController

查看:24
本文介绍了使图像可从其他 ViewController 访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想知道使名为 profpic 的图像可供我制作或打算制作的所有其他 ViewController 访问的代码.我已经阅读了许多关于全局变量、公共变量和其他尚未生效的建议的帖子.如果有帮助,我专门使用它来显示来自 ViewControllerA 的图像作为 ViewControllerB 的背景.

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.

推荐答案

您可以使用单例类并将 UIImage 放在上面.在ViewControllerA中设置,在ViewControllerB

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

访问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 访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆