我一直在尝试将主包中的图像和文本文件加载到UITextView和UIImageView框中。我已经从.xib文件正确连接了出口,并确保我尝试加载的文件存在于捆绑包中,但它们没有加载,当我通过NSLog测试它们时它们返回为null。下面复制的是.h和.m文件中的代码。如果你想知道,有些网点很强大,因为我一直试图弄乱这些设置让它发挥作用,到目前为止无济于事。
“。h file”
@interface CluesViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *cluesBackground;
@property (weak, nonatomic) IBOutlet UIImageView *titleImage;
@property (weak, nonatomic) IBOutlet UIImageView *typeATitle;
@property (weak, nonatomic) IBOutlet UIImageView *typeBTitle;
@property (weak, nonatomic) IBOutlet UIImageView *divider;
@property (weak, nonatomic) IBOutlet UITextView *typeAText;
@property (strong, nonatomic) IBOutlet UITextView *typeBText;
-(void)updateCluesViewInfoTypeA:(NSString *)typeA updateCluesViewInfoTypeB:(NSString *)typeB;
@end
“。m file”
-(void)updateCluesViewInfoTypeA:(NSString *)typeA updateCluesViewInfoTypeB:(NSString *)typeB {
NSString *bGImageString = NULL;
bGImageString = [NSString stringWithFormat:@"%@&%@-bg", typeA, typeB];
NSString *bGPath = [[NSBundle mainBundle] pathForResource:bGImageString ofType:@"png"];
UIImage *bGImage = [UIImage imageNamed:bGPath];
[_cluesBackground setImage:bGImage];
NSString *titleImageString = NULL;
titleImageString = [NSString stringWithFormat:@"%@&%@-clues", typeA, typeB];
NSString *titleImagePath = [[NSBundle mainBundle] pathForResource:titleImageString ofType:@"png"];
UIImage *titleImage = [UIImage imageNamed:titleImagePath];
[_titleImage setImage:titleImage];
NSString *typeATitleImageString = NULL;
typeATitleImageString = [NSString stringWithFormat:@"%@-text", typeA];
NSString *typeATitlePath = [[NSBundle mainBundle] pathForResource:typeATitleImageString ofType:@"png"];
UIImage *typeATitleImage = [UIImage imageNamed:typeATitlePath];
[_typeATitle setImage:typeATitleImage];
NSString *typeBTitleImageString = NULL;
typeBTitleImageString = [NSString stringWithFormat:@"%@-text", typeB];
NSString *typeBTitlePath = [[NSBundle mainBundle] pathForResource:typeBTitleImageString ofType:@"png"];
UIImage *typeBTitleImage = [UIImage imageNamed:typeBTitlePath];
[_typeBTitle setImage:typeBTitleImage];
NSString *dividerImageString = NULL;
dividerImageString = [NSString stringWithFormat:@"%@&%@-divider", typeA, typeB];
NSString *dividerImagePath = [[NSBundle mainBundle] pathForResource:dividerImageString ofType:@"png"];
UIImage *dividerImage = [UIImage imageNamed:dividerImagePath];
[_divider setImage:dividerImage];
NSString *typeATextString = NULL;
typeATextString = [NSString stringWithFormat:@"%@Clues", typeA];
NSString *typeATextPath = [[NSBundle mainBundle] pathForResource:typeATextString ofType:@"txt"];
NSString* typeATextContent = [NSString stringWithContentsOfFile:typeATextPath encoding:NSASCIIStringEncoding error:NULL];
self.typeAText.text = typeATextContent;
NSString *typeBTextString = NULL;
typeBTextString = [NSString stringWithFormat:@"%@Clues", typeB];
NSString *typeBTextPath = [[NSBundle mainBundle] pathForResource:typeBTextString ofType:@"txt"];
NSString* typeBTextContent = [NSString stringWithContentsOfFile:typeBTextPath encoding:NSASCIIStringEncoding error:NULL];
self.typeBText.text = typeBTextContent;
NSLog(@"The textfield is %@", self.typeBText.text);
}
答案 0 :(得分:1)
您错误地使用了imageNamed:
。你应该传递图像名称:
NSString *bGImageString = [NSString stringWithFormat:@"%@&%@-bg", typeA, typeB];
UIImage *bGImage = [UIImage imageNamed:bGImageString];
也可以对其他图像进行此更改。您只想使用UIImage imageWithContentsOfFile:
而不是imageNamed:
来使用图片的完整路径。
对于文件,您确定文件编码是ASCII而不是UTF-8吗?
如果编码不是问题,请使用error
参数查看问题所在。
NSString *typeATextString = [NSString stringWithFormat:@"%@Clues", typeA];
NSString *typeATextPath = [[NSBundle mainBundle] pathForResource:typeATextString ofType:@"txt"];
NSError *error = nil;
NSString* typeATextContent = [NSString stringWithContentsOfFile:typeATextPath encoding:NSASCIIStringEncoding error:&error];
if (typeATextContent) {
self.typeAText.text = typeATextContent;
} else {
NSLog(@"Unable to load text from %@: %@", typeATextPath, error);
}
答案 1 :(得分:0)
尝试按nonatomic
创建出口,例如......
@property (nonatomic,retain) IBOutlet UITextView *typeBText;