我目前正在开发一款使用UIAlertview
更改按钮图片的应用。
我试过这个,但它似乎没有工作!
请帮帮忙!
拉菲
- (IBAction)changebackground:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"The Title"
message:@"The Message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"normal",@"for girly girls",@"for boys!", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
UIImage *img = [UIImage imageNamed:@"img1.jpg"];
[background setImage:img];
UIImage *buttonImage = [UIImage imageNamed:@"black sharp.png"];
[asharp setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:asharp];
UIImage *buttonImage2 = [UIImage imageNamed:@"black sharp.png"];
[csharp setImage:buttonImage2 forState:UIControlStateNormal];
[self.view addSubview:csharp];
UIImage *buttonImage3 = [UIImage imageNamed:@"black sharp.png"];
[dsharp setImage:buttonImage3 forState:UIControlStateNormal];
[self.view addSubview:dsharp];
UIImage *buttonImage4 = [UIImage imageNamed:@"black sharp.png"];
[fsharp setImage:buttonImage4 forState:UIControlStateNormal];
[self.view addSubview:fsharp];
UIImage *buttonImage5 = [UIImage imageNamed:@"black sharp.png"];
[gsharp setImage:buttonImage5 forState:UIControlStateNormal];
[self.view addSubview:gsharp];
UIImage *buttonImage6 = [UIImage imageNamed:@"white key.png"];
[middlec setImage:buttonImage6 forState:UIControlStateNormal];
[self.view addSubview:middlec];
UIImage *buttonImage7 = [UIImage imageNamed:@"white key.png"];
[d setImage:buttonImage7 forState:UIControlStateNormal];
[self.view addSubview:d];
UIImage *buttonImage8 = [UIImage imageNamed:@"white key.png"];
[en setImage:buttonImage8 forState:UIControlStateNormal];
[self.view addSubview:en];
UIImage *buttonImage9 = [UIImage imageNamed:@"white key.png"];
[f setImage:buttonImage9 forState:UIControlStateNormal];
[self.view addSubview:f];
UIImage *buttonImage10 = [UIImage imageNamed:@"white key.png"];
[g setImage:buttonImage10 forState:UIControlStateNormal];
[self.view addSubview:g];
UIImage *buttonImage11 = [UIImage imageNamed:@"white key.png"];
[a setImage:buttonImage11 forState:UIControlStateNormal];
[self.view addSubview:a];
UIImage *buttonImage12 = [UIImage imageNamed:@"white key.png"];
[b setImage:buttonImage12 forState:UIControlStateNormal];
[self.view addSubview:b];
}
if (buttonIndex == 2) {
UIImage *img = [UIImage imageNamed:@"img2.jpg"];
[background setImage:img];
UIImage *buttonImage = [UIImage imageNamed:@"green sharp.png"];
[asharp setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:asharp];
UIImage *buttonImage2 = [UIImage imageNamed:@"green sharp.png"];
[csharp setImage:buttonImage2 forState:UIControlStateNormal];
[self.view addSubview:csharp];
UIImage *buttonImage3 = [UIImage imageNamed:@"green sharp.png"];
[dsharp setImage:buttonImage3 forState:UIControlStateNormal];
[self.view addSubview:dsharp];
UIImage *buttonImage4 = [UIImage imageNamed:@"green sharp.png"];
[fsharp setImage:buttonImage4 forState:UIControlStateNormal];
[self.view addSubview:fsharp];
UIImage *buttonImage5 = [UIImage imageNamed:@"green sharp.png"];
[gsharp setImage:buttonImage5 forState:UIControlStateNormal];
[self.view addSubview:gsharp];
UIImage *buttonImage6 = [UIImage imageNamed:@"pink key.png"];
[middlec setImage:buttonImage6 forState:UIControlStateNormal];
[self.view addSubview:middlec];
UIImage *buttonImage7 = [UIImage imageNamed:@"pink key.png"];
[d setImage:buttonImage7 forState:UIControlStateNormal];
[self.view addSubview:d];
UIImage *buttonImage8 = [UIImage imageNamed:@"pink key.png"];
[en setImage:buttonImage8 forState:UIControlStateNormal];
[self.view addSubview:en];
UIImage *buttonImage9 = [UIImage imageNamed:@"pink key.png"];
[f setImage:buttonImage9 forState:UIControlStateNormal];
[self.view addSubview:f];
UIImage *buttonImage10 = [UIImage imageNamed:@"pink key.png"];
[g setImage:buttonImage10 forState:UIControlStateNormal];
[self.view addSubview:g];
UIImage *buttonImage11 = [UIImage imageNamed:@"pink key.png"];
[a setImage:buttonImage11 forState:UIControlStateNormal];
[self.view addSubview:a];
UIImage *buttonImage12 = [UIImage imageNamed:@"pink key.png"];
[b setImage:buttonImage12 forState:UIControlStateNormal];
[self.view addSubview:b];
}
if (buttonIndex == 3) {
UIImage *img = [UIImage imageNamed:@"img3.jpg"];
[background setImage:img];
UIImage *buttonImage = [UIImage imageNamed:@"orange sharp.png"];
[asharp setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:asharp];
UIImage *buttonImage2 = [UIImage imageNamed:@"orange sharp.png"];
[csharp setImage:buttonImage2 forState:UIControlStateNormal];
[self.view addSubview:csharp];
UIImage *buttonImage3 = [UIImage imageNamed:@"orange sharp.png"];
[dsharp setImage:buttonImage3 forState:UIControlStateNormal];
[self.view addSubview:dsharp];
UIImage *buttonImage4 = [UIImage imageNamed:@"orange sharp.png"];
[fsharp setImage:buttonImage4 forState:UIControlStateNormal];
[self.view addSubview:fsharp];
UIImage *buttonImage5 = [UIImage imageNamed:@"orange sharp.png"];
[gsharp setImage:buttonImage5 forState:UIControlStateNormal];
[self.view addSubview:gsharp];
UIImage *buttonImage6 = [UIImage imageNamed:@"silver key.png"];
[middlec setImage:buttonImage6 forState:UIControlStateNormal];
[self.view addSubview:middlec];
UIImage *buttonImage7 = [UIImage imageNamed:@"silver key.png"];
[d setImage:buttonImage7 forState:UIControlStateNormal];
[self.view addSubview:d];
UIImage *buttonImage8 = [UIImage imageNamed:@"silver key.png"];
[en setImage:buttonImage8 forState:UIControlStateNormal];
[self.view addSubview:en];
UIImage *buttonImage9 = [UIImage imageNamed:@"silver key.png"];
[f setImage:buttonImage9 forState:UIControlStateNormal];
[self.view addSubview:f];
UIImage *buttonImage10 = [UIImage imageNamed:@"silver key.png"];
[g setImage:buttonImage10 forState:UIControlStateNormal];
[self.view addSubview:g];
UIImage *buttonImage11 = [UIImage imageNamed:@"silver key.png"];
[a setImage:buttonImage11 forState:UIControlStateNormal];
[self.view addSubview:a];
UIImage *buttonImage12 = [UIImage imageNamed:@"silver key.png"];
[b setImage:buttonImage12 forState:UIControlStateNormal];
[self.view addSubview:b];
}
}
答案 0 :(得分:1)
我刚刚碰到了这个。按钮不能更改其背景,除非它们是自定义的。否则,更改常规按钮的背景就像修改UITableViewCell
的背景一样(访问contentView
时会发生实际更改 - 但UIButton
不是这种情况)
因此,将按钮类型从界面构建更改为Custom
,或者从以下代码初始化按钮:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];