2015-07-01 97 views
0

我想從我的.plist中加載數據到我以編程方式創建的按鈕。我的plist文件包含圖像。我希望將圖像添加到我在調用.plist時創建的按鈕上。目前,我只能獲取圖像名稱以顯示在屏幕上。這是我嘗試過的。從Plist文件設置按鈕圖像

NSString *path = [[NSBundle mainBundle] pathForResource:@"Academic" ofType:@"plist"]; 
    NSArray *buttonsArray = [[NSArray alloc] initWithContentsOfFile:path]; 

    NSLog(@"%@",buttonsArray); 

    int positionX = 10; 

    for (int i = 0; i < [buttonsArray count] ; i++) { 

     MainMenu *abingtonAcademic = [buttonsArray objectAtIndex:i]; 

     UIButton *abAcademicButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [abAcademicButton setImage:abingtonAcademic forState:UIControlStateNormal]; 
     abAcademicButton.frame = CGRectMake(0, positionX, 300, 40); 
     [self.mainMenuScroll addSubview:abAcademicButton]; 

     positionX += 50; 

    } 

    [self.mainMenuScroll setContentSize:CGSizeMake(320, positionX + 20)]; 

} 
+1

當你有像名字你應該使用'+(NSImage *)imageNamed:(NSString *)name'來創建圖像,然後將其設置爲按鈕的圖像。 (前提是您的資產中包含此名稱的圖片) – nanako

回答

0

我懷疑,你沒有圖像的plist,但它的名字。所以,你需要將圖像放在應用程序的包中。不是,你可以設置你的按鈕圖像與

[abAcademicButton setImage:[UIImage imageNamed: abingtonAcademic] forState:UIControlStateNormal]; 

但是,如果你有你的圖像編碼爲NSData的並存儲到plist中,比你應該使用

[abAcademicButton setImage:[UIImage imageWithData: abingtonAcademic] forState:UIControlStateNormal];