我一直在嘗試從主包加載圖像和文本文件到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);
}
什麼時候調用'updateCluesViewInfoTypeA:updateCluesViewInfoTypeB:'?它必須在調用'viewDidLoad'後調用。 – rmaddy
它在viewDidLoad之後調用。 – NBAfan