简体   繁体   中英

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. 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 (*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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM