[英]iphone sdk conditional in switch function
I'm trying to make a random image appear on the press of a button. 我正在尝试使随机图像出现在按钮上。 So it generates a random number, and the switch algorithm swaps the chosen image with the one in the imgview.
因此,它生成一个随机数,并且切换算法将所选图像与imgview中的图像交换。 but I want a switch in the settings app to toggle which set of images to use.
但我想在设置应用中进行切换,以切换要使用的图像集。 I know pretty much how to do it...it's just that it doesn't work.
我非常知道该怎么做...只是不起作用。 I'm missing some syntax thing...
我缺少一些语法的东西...
int Number = rand() %30;
NSString *toggleValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"enabled_preference"];
switch (Number) {
if (*toggleValue == 0) {
case 0:
picture.image = [UIImage imageNamed:@"1.png"];
break;
case 1:
picture.image = [UIImage imageNamed:@"2.png"];
break;}
else {
case 0:
picture.image = [UIImage imageNamed:@"3.png"];
break;
case 1:
picture.image = [UIImage imageNamed:@"4.png"];
break;}
}
NSString *toggleValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"enabled_preference"];
NSArray *imagesA = [NSArray arrayWithObjects:@"img1.png" , @"img2.png" , ... , nil];
NSArray *imagesB = [NSArray arrayWithObjects:@"img8.png" , @"img9.png" , ... , nil];
NSArray *images = [toggleValue integerValue] ? imagesA : imagesB;
NSString *name = [images objectAtIndex:rand() % [images count]];
picture.image = [UIImage imageNamed:name];
You can't put an if into a switch like this... try with this syntax instead: 您不能将if放入这样的开关中,而是尝试使用以下语法:
if (*toggleValue == 0)
{
switch (Number)
{
case 0:picture.image = [UIImage imageNamed:@"1.png"]; break;
case 1:picture.image = [UIImage imageNamed:@"2.png"];break;
}
}
else
{
switch (Number)
{
case 0:picture.image = [UIImage imageNamed:@"3.png"];break;
case 1:picture.image = [UIImage imageNamed:@"4.png"];break;
}
}
int randomInt = rand() % 30;
if (toggleEnabled) {
switch (randomInt) {
case 0:
picture.image = [UIImage imageNamed:@"0.toggleEnabled.png"];
break;
case 1:
picture.image = [UIImage imageNamed:@"1.toggleEnabled.png"];
break;
// ...
}
else {
switch (randomInt) {
case 0:
picture.image = [UIImage imageNamed:@"0.toggleDisabled.png"];
break;
// ...
}
}
I am thinking your searching for a way to just use the number to open the image.. 我在想您正在寻找一种仅使用数字打开图像的方法。
If this is the case, this is the code: 在这种情况下,代码如下:
NSString *theImage = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d.toggleEnabled", sliceIndex] ofType:@"png"]; picture.image = [[UIImage alloc] initWithContentsOfFile: theImage];
if you named your images differently, you could also have it handle the toggle. 如果您以不同的方式命名图像,则也可以让它处理切换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.