【发布时间】:2012-09-23 23:07:58
【问题描述】:
我使用了一个只包含一个传递值的单例类。然后我尝试添加另一个。
#import <Foundation/Foundation.h>
@interface GlobalValueContainer : NSObject {
NSString *passedText;
NSString *myPassedPictureName;
}
@property (nonatomic, strong) NSString* passedText;
@property (nonatomic, strong) NSString* myPassedPictureName;
+ (GlobalValueContainer *) sharedStore;
@end
#import "GlobalValueContainer.h"
@implementation GlobalValueContainer;
@synthesize passedText;
@synthesize myPassedPictureName;
static GlobalValueContainer *sharedStore = nil;
+ (GlobalValueContainer *) sharedStore {
@synchronized(self){
if (sharedStore == nil){
sharedStore = [[self alloc] init];
}
}
return sharedStore;
}
@end
然后从第一个视图我尝试设置myPassedPictureName
-(IBAction)setPicture:(id)sender{
myPicture = @"Hus";
GlobalValueContainer* localContainer = [GlobalValueContainer sharedStore];
localContainer.myPassedPictureName = myPicture;
}
在第二个视图中,我想设置一个具有该名称的图像视图(即 +png)
- (void)viewDidLoad
{
[super viewDidLoad];
//Store* myStore = [Store sharedStore];
GlobalValueContainer* localContainer = [GlobalValueContainer sharedStore];
myPassedPictureName = localContainer.myPassedPictureName;
myPicture.image = [UIImage imageNamed:myPassedPictureName];
whatFile.text = myPassedPictureName;
//object.imageView.image = [UIImage imageNamed:@"downloadedimage.png"];
}
图片不显示。我还尝试添加 UIlabel 并设置应该传递的字符串。但它也变成了空白。
当我使用“passedText”传递文本时,它工作正常。当我添加第二个 NSString 时,什么都没有发生?
首先要做的事情。任何人都可以看到有什么问题(仍然在这里学习 obj c :) 并且,这是我尝试操纵 UIImageView 的正确方法吗?我想使用myPassedPictureName 在多个UIViews 上设置图片,具体取决于按下的按钮。
期待您的意见。
【问题讨论】:
-
请说明是什么语言。
-
哦 - 对不起 :) 这是 xcode 编辑器中的目标 c
-
我看不出您的代码有任何明显错误。您是否尝试过在该方法中检查变量的值?您是否尝试过手动将其替换为图像名称以确认您没有弄错名称?
-
我认为我的单身人士“混乱”了,它无法处理两个变量。 *passedText 工作正常 - 我们使用“全部” myPassedPictureName remount null 即使我尝试在 IBAction 中设置它
-
我认为您并不完全了解 Objective-C 属性的用途、委托协议设计模式以及单例的正确用例是什么。单例是存储状态的简单方法。将信息从一个对象直接传递到另一个对象通常不会那么脆弱,而且更有意义。
标签: objective-c xcode singleton