han*_*Dev 0 iphone objective-c switch-statement ios
我试图将以下代码从if语句更改为switch语句,但我得到一个错误,说我需要一个整数而不是id对象.
这是我的开关:
-(void) nearestIndexAfterRotationEnded:(int)index {
NSLog(@"Selected item is: %@", [colorNames objectAtIndex:index]);
statusLabel.text = [NSString stringWithFormat:@"Selected item is: %@", [colorNames objectAtIndex:index]];
switch ([colorNames objectAtIndex:])) {
case 1:
[redButton setImage:[UIImage imageNamed:@"Btn1_1.png"] forState:UIControlStateNormal];
break;
case 2:
[redButton setImage:[UIImage imageNamed:@"Btn1_2.png"] forState:UIControlStateNormal];
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
这是我的if条件声明:
-(void) nearestIndexAfterRotationEnded:(int)index {
NSLog(@"Selected item is: %@", [colorNames objectAtIndex:index]);
statusLabel.text = [NSString stringWithFormat:@"Selected item is: %@", [colorNames objectAtIndex:index]];
if ([colorNames objectAtIndex:0]) {
[redButton setImage:[UIImage imageNamed:@"Btn1_1.png"] forState:UIControlStateNormal];
}
if ([colorNames objectAtIndex:1]) {
[redButton setImage:[UIImage imageNamed:@"Btn1_2.png"] forState:UIControlStateNormal];
}
if ([colorNames objectAtIndex:2]) {
[redButton setImage:[UIImage imageNamed:@"Btn1_3.png"] forState:UIControlStateNormal];
}
if ([colorNames objectAtIndex:3]) {
[redButton setImage:[UIImage imageNamed:@"Btn1_4.png"] forState:UIControlStateNormal];
}
if ([colorNames objectAtIndex:4]) {
[redButton setImage:[UIImage imageNamed:@"Btn1_5.png"] forState:UIControlStateNormal];
}
if ([colorNames objectAtIndex:5]) {
[redButton setImage:[UIImage imageNamed:@"Btn1_6.png"] forState:UIControlStateNormal];
}
if ([colorNames objectAtIndex:6]) {
[redButton setImage:[UIImage imageNamed:@"Btn1_7.png"] forState:UIControlStateNormal];
}
if ([colorNames objectAtIndex:7]) {
[redButton setImage:[UIImage imageNamed:@"Btn1_8.png"] forState:UIControlStateNormal];
}
if ([colorNames objectAtIndex:8]) {
[redButton setImage:[UIImage imageNamed:@"Btn1_9.png"] forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)
}
谢谢你的帮助
- (void)nearestIndexAfterRotationEnded:(int)index {
NSLog(@"Selected item is: %@", [colorNames objectAtIndex:index]);
statusLabel.text = [NSString stringWithFormat:@"Selected item is: %@", [colorNames objectAtIndex:index]];
for (int i=0; i<9; i++) {
if ([colorNames objectAtIndex:i]) {
[redButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Btn1_%d.png", i+1]] forState:UIControlStateNormal];
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
224 次 |
| 最近记录: |