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