我正在尝试将数据从我的.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)];
}
答案 0 :(得分:0)
我怀疑,你没有plist中的图像,而是它的名字。因此,您需要将图像放在应用程序包中。比,您可以使用
设置按钮图像[abAcademicButton setImage:[UIImage imageNamed: abingtonAcademic] forState:UIControlStateNormal];
但是,如果您将图像编码为NSData并存储到plist,则应使用
[abAcademicButton setImage:[UIImage imageWithData: abingtonAcademic] forState:UIControlStateNormal];