1

I have two ViewControllers.

FirstViewController has UILabels and UIImageViews. Also generated a Singleton from its class. SecondViewController has a call of that Singleton and should GET values of UILabels and UIImageViews from FirstViewController.

NSString works, UILabel do not.

For example, access a NSString in SecondviewController with "sharedFirstViewController.nsstringvarname" I can GET and SET the value. This works fine!

Trying the same with an UILabel like this: "sharedFirstViewController.uilabelvar.text" I doens´t can SET or GET the value of this UILable. This works NOT!

Is there any special thing with UI-Objects? Would be great if someone can help me with that. Thanks!

EDIT:

in FirstViewController.h

+ (id)sharedFirstViewController; 
@property (copy, nonatomic) NSString *path; 
@property (strong, nonatomic) IBOutlet UIImageView *pic;

in 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;
    }

in 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

}
4

1 回答 1

0

我确定您忘记在单例类的 init 方法中分配给 UIImageview *pic 。那是因为您的代码正在运行但没有任何崩溃和警告。如果您调试代码,您将获得 imageview 内存分配 0X0。核实。

- (id)init {
    if (self = [super init]) {
         _pic = [[UIImageView alloc] initWithFrame:CGRectMake(0,  0, 100, 200)];
    }
    return self;
}

在 FirstViewController.m 类中编写上述方法。他们不需要在 UIImageView *pic 前面写 IBOutlet 因为你在生成单例实例时没有提到你的视图控制器笔尖。每当您在 xib 中处理 UI 时,请使用 IBOutlet。

于 2013-11-01T17:06:40.187 回答