2013-11-28 108 views
0

我一直在尝试从主包加载图像和文本文件到UITextView和UIImageView框中。我已经从.xib文件中正确地连接了插座,并确保我尝试加载的文件存在于软件包中,但它们没有加载,并且当我通过NSLog测试它们时,它们返回为空。下面复制的是.h和.m文件中的代码。如果你想知道,一些网点是强大的,因为我一直在试图搞乱设置才能使其运行起来,至今无济于事。无法以编程方式将文件从捆绑包加载到UITextView和UIImageView

“.h文件中”

@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文件”

-(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

什么时候调用'updateCluesViewInfoTypeA:updateCluesViewInfoTypeB:'?它必须在调用'viewDidLoad'后调用。 – rmaddy

+0

它在viewDidLoad之后调用。 – NBAfan

回答

0

尝试通过nonatomic,喜欢创造店......

@property (nonatomic,retain) IBOutlet UITextView *typeBText; 
+1

为什么要帮忙?他使用的代码是由Xcode生成的代码,可以工作。 – Marc

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); 
} 
+0

我尝试使用您建议的代码,但错误也会返回为null。此外,我检查了typeatxtContent变量,它用我打算的文本文件加载。 – NBAfan

+0

如果'typeATextContent'具有正确的值,那就很好。如果文本没有出现在文本视图中,那么这意味着'self.typeAText'是'nil'。您的插座不能正确连接。 – rmaddy

相关问题