2013-11-28 80 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

相關問題