0

I need some help with my code. I have an UIAlert that pops up the first time that you open the app, in that pop up I have two buttons and the user will choose one of those. I want the app to remember what button the user chose to execute some code or other. The thing is I have this code right here :

-(void)changeLabel{


        progressView.progress += 0.25;
    scan.hidden = YES;


        if (progressView.progress == 1 ) {
            label.hidden = YES;

            progressView.hidden = YES;

            [timer invalidate];

            imagesText.hidden = NO;


            int randomNumber = arc4random() % 4;

            switch (randomNumber) {
                case 0:


                    imagesText.image = [UIImage imageNamed:@"image1.png"];


                    break;

                case 1:


                    imagesText.image = [UIImage imageNamed:@"image2.png"];


                    break;

                case 2:

                    imagesText.image = [UIImage imageNamed:@"image3.png"];

                    break;
                case 3:

                    imagesText.image = [UIImage imageNamed:@"image4.png"];

                default:
                    break;

        }
    }
}

So I want to make it in some way that if the user selected the first button the app will do the switch between cases 0,1 and 2 and if he selected the second button it'll do it between 3 and others. But I want the beginning of the code to be the same for both cases. I tried some stuff but it doesn't work how I wanted. Thank you for your help!

UPDATE: I got the error fixed. So right now I have this :

- (void)viewDidLoad
{


    if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"alert"] ]) {
        [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"alert"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        UIAlertView *alerta = [[UIAlertView alloc]initWithTitle:@"Welcome!" message:@"Select your language" delegate:self cancelButtonTitle:@"Button1" otherButtonTitles:@"Button2", nil];



        [alerta show];
        [alerta release];

    }


    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];



   progressView.progress += 0.25;
    scan.hidden = YES;


    if (progressView.progress == 1 ) {
        label.hidden = YES;

        progressView.hidden = YES;

        [timer invalidate];

        imagesText.hidden = NO;

        int randomNumber;

        if ([buttonTitle isEqualToString:@"Button1"]) {

             randomNumber = arc4random() % 3;

        }else if ([buttonTitle isEqualToString:@"Button2"]) {

             randomNumber = arc4random() % 3 + 3;

        }

        switch (randomNumber) {
           //CODE
        }
    }
}

The problem is that all that code is supposed to be called from :

 - (IBAction)scan:(id)sender {

    label.hidden = NO;
    imagesText.hidden = YES;
    progressView.hidden = NO;


    progressView.progress = 0;

    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(alertView) userInfo:nil repeats:YES];

}

So I changed the @selector to the alertView one and now when I press the button that execute all the code it gives me that SIGABRT error. Any ideas why? Thank you for all your help!

6
  • You don't have (or you're not showing) a selector called "alertView"
    – rdelmar
    May 21, 2012 at 5:36
  • So how can I make all that code work when I press the button? Because it needs to be inside the uialertview right? May 21, 2012 at 5:42
  • I'm not sure what you're doing now -- the alertView:clickedButtonAtIndex: method is a delegate method that's called by UIAlert when you click a button, you don't call it explicitly (if that's what you're trying to do in the timer method). You need to create a property buttonTitle so it can be used across different methods -- the only thing that needs to be in the delegate method is self.buttonTitle = [alertView buttonTitleAtIndex:buttonIndex]; Then you would use buttonTitle like I showed in my answer.
    – rdelmar
    May 21, 2012 at 5:46
  • Oh I got it now, I'll try tomorrow and I'll tell you if it worked. Thanks for your time May 21, 2012 at 5:50
  • Do I have to declare buttonTitle before or something? I tried making a property on the .h file I put: @property (nonatomic, retain) UIAlertView *buttonTitle; and then I synthesized it but I get again the SIGABRT error when I press the button that execute everything May 21, 2012 at 20:09

1 Answer 1

0

When the user chooses a button, you need to populate a property with some unique value of the button, like its title or a tag. Once you have that, you can put an if statement before your randomNumber definition -- something like this:

if (buttonTitle isEqualToString:@"First Button") {
        int randomNumber = arc4random() % 3; // Gives choices of 0,1, or 2
    }else if (buttonTitle isEqualToString:@"Second Button"){
        int randomNumber = arc4random() % 3 + 3; // Gives choices of 3,4, or 5
    }

     switch (randomNumber) .... (rest of your code)

In the else-if statement the +3 will ensure that the choices in the switch statement will start with 3 and go up from there (I don't know how many other choices you have).

5
  • I understand what you said, but how do I make that property for the button? because right now the buttons appear on the UIAlertView.Sorry if it's a stupid question but I still kind of newby. May 21, 2012 at 4:08
  • In the UIAlertView delegate method,alertView:clickedButtonAtIndex:, you'll get the index of the button clicked. You could save that index to a property.
    – rdelmar
    May 21, 2012 at 4:18
  • Alright, when I put all the code together it says : "Use of undeclared identifier 'randomNumber'" Do i have to make a variable with randomNumber somewhere? if so, where? May 21, 2012 at 4:41
  • Oh, sorry, that's my fault -- you should declare the variable above the if statement, so have a line that's just: int randomNumber; Then in the if statement just use randomNumber without the "int" in front of it.
    – rdelmar
    May 21, 2012 at 4:54
  • That worked, I updated the questions. Check it out if you have some time :) May 21, 2012 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.