我有两个ViewControllers。如何通过Singleton设置/获取UIImageView.image?
FirstViewController具有UILabels和UIImageViews。也从它的类生成一个单例。 SecondViewController具有该Singleton的调用,并且应该从FirstViewController获取UILabels和UIImageView的值。
NSString工作,UILabel不。
例如,访问的NSString在SecondviewController与“sharedFirstViewController.nsstringvarname”我可以获取和设置值。 这工作正常!
试图与的UILabel这样的一样:“sharedFirstViewController.uilabelvar.text”我好好尝试一下可以设置或获取该UILable的价值。 这不适用!
UI-Objects有什么特别之处吗? 如果有人能帮助我,那会很棒。 谢谢!
编辑:
在FirstViewController.h+ (id)sharedFirstViewController;
@property (copy, nonatomic) NSString *path;
@property (strong, nonatomic) IBOutlet UIImageView *pic;
在FirstViewController.m
@synthesize path= _path;
@synthesize pic = _pic;
- (id)init {
if (self = [super init]) {
_pic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
}
return self;
}
+ (id) sharedFirstViewController {
static sharedFirstViewController * sharedFirstViewController = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedFirstViewController = [[self alloc] init];
});
return sharedFirstViewController;
}
在SecondViewController.m
#import "FirstViewController.h"
- (void)viewDidLoad
{
FirstViewController * sharedFirstViewController = [FirstViewController sharedFirstViewController];
NSLog(@"this is NSString from FVC: %@" sharedFirstViewController.path); //works
UIImage * picture = [UIImage imageWithContentsOfFile:sharedFirstViewController.path];
sharedFirstViewController.pic.image = picture; // does´t work
}
代码和具体的错误将有助于解决什么是错误的 – wattson12
显示你的代码你如何使单身人士类,它的变量 – Tirth