Need some help on an if statement

Discussion in 'iOS Programming' started by Jayman29, Nov 29, 2010.

  1. Jayman29 macrumors newbie

    Joined:
    Nov 29, 2010
    #1
    My program is made up of someone's face that needs to be clicked and then it gives a win condition. Simple as it is my program has a big problem. It says correct and generates a new part. That part is fine and would be great if it stopped there but it sends another correct message.

    So, Simply my problem is this the program gives the correct message twice. I put my code in below for one of the parts. All of them are similar just with the if statement being equal to different numbers.


    Code:
    -(IBAction)btnMouth;{
    	if (myRandomNumber == 3){
    	
    	UIAlertView *playerWin4 = [[UIAlertView alloc] initWithTitle: @"Correct" message: @"You Selected the Mouth." delegate: nil cancelButtonTitle: @"OK" otherButtonTitles: nil];
    	[playerWin4 show];
    	
    	}
    myRandomNumber = arc4random() % 4;
    	
    	if (myRandomNumber == 0){
    		UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select the Nose." delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    		[playerAtt show];
    	}		
    	if (myRandomNumber == 1){
    		UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select an Eye" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    		[playerAtt show];
    	}
    	if (myRandomNumber == 2){
    		UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select an Ear" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    		[playerAtt show];
    	}
    	if (myRandomNumber == 3){
    		UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select the Mouth" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    		[playerAtt show];
    	}
    	
    }
    Thank you in advance for any help.
     
  2. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #2
    You need to explain more about what your overall goal is. There's nothing wrong programatically that I can see, but what you're trying to do isn't clear.

    One thing I could suggest regardless of what you're trying to do is change the last 3 if statements at the end of that segment to be "else if" statements. That way the statements after the first successful one will be ignored. That has nothing to do with your problem (need to know more about what you're trying to do), it's just a suggestion.

    Also, if you don't want pushing one button to randomly throw two alert views when a certain number is selected, you could put the code segment below the first if statement inside an else statement. But again, don't know enough about your goal to know if that's a good idea or not.
     
  3. shadyInCA macrumors member

    Joined:
    Jun 20, 2008
    #3
    I agree with cnstoll, you should use "else if" for the ==1, ==2 and ==3 checks just for good coding practice (and performance).

    Also, if you only want to generate a part if the user was successful, then you need to close the first if block right at the end of btnMouth.

    None of this solves your problem though.
    If I had to guess, I'd say you should look at what actions you've connected the bntMouth Action to.
     
  4. Jayman29 thread starter macrumors newbie

    Joined:
    Nov 29, 2010
    #4
    I am not sure what you mean by me needing to be more clear. I am going to give it a try. The code is simple, it is a program that shows a face(btw this is a view based app) with 4 different buttons on the face. The buttons are spread out over the different parts of the face. Two buttons, one for each eye. One for each ear, one for the mouth, and one for the nose. When the program starts it generates a number.

    Code:
    - (void)viewDidLoad {
    	myRandomNumber = arc4random() % 4;
    	
    	if (myRandomNumber == 0){
    		UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select the Nose." delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    		[playerAtt show];
    	}		
    	if (myRandomNumber == 1){
    		UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select an Eye" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    		[playerAtt show];
    	}
    	if (myRandomNumber == 2){
    		UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select an Ear" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    		[playerAtt show];
    	}
    	if (myRandomNumber == 3){
    		UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select the Mouth" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    		[playerAtt show];
    	}
    }
    
    So now the User needs to choose the piece that was randomly generated. The player now clicks one of the set up buttons. The buttons are set up like the original one I posted. The user clicks the button and if the bool is true then it gives the win condition. In order to keep the app from stopping there I added the same exact code from view did load to each button.

    Now the problem is when you click the button it comes up with a win message(like it should) then it moves on to the generated part(Perfect) then out of no where it shows the win message again. That is the program, I will save the long line of code for each button because each button is the same except the if statement is different. I will give another sample for all to see.

    Example:
    Code:
    -(IBAction)btnEye_L;{
    	
    	if (myRandomNumber == 1){
    	
    	UIAlertView *playerWin2 = [[UIAlertView alloc] initWithTitle: @"Correct" message: @"You Selected an Eye." delegate: nil cancelButtonTitle: @"OK" otherButtonTitles: nil];
    	[playerWin2 show];
        
    	}
    myRandomNumber = arc4random() % 4;
    		
    		if (myRandomNumber == 0){
    			UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select the Nose." delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    			[playerAtt show];
    		}		
    		else if (myRandomNumber == 1){
    			UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select an Eye" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    			[playerAtt show];
    		}
    		else if (myRandomNumber == 2){
    			UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select an Ear" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    			[playerAtt show];
    		}
    		else if (myRandomNumber == 3){
    			UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select the Mouth" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    			[playerAtt show];
    		}
    	
    	
    	
    }
    
    I added the else if parts and that didn't help. Again, the problem is that the program gives a correct message, then a new part, then another correct(that extra correct is the problem).

    If you need another piece of code then you need to tell me. This is pretty much the whole code so I don't know what else to add short of giving you my project images and all.
     
  5. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #5
    I assume you have your buttons configured in interface builder? What are the button selectors that point to those IBActions in interface builder?

    Depending on how you set that up that method could be getting called twice. That's the only way this could be happening.

    Set a breakpoint at the beginning of each IBAction method and click "debug" in XCode. Then click a button in the simulator and click "step over" in the debug window to go through your code line by line and see if the method gets called twice for one button press.
     
  6. Jayman29 thread starter macrumors newbie

    Joined:
    Nov 29, 2010
    #6
    Sorry it took me so long to get back to you. It only goes through the button once. I thing the problem is this: The button is called it puts up correct and then instead of waiting for the user to click ok it moves on to the next UIAlertView. Then You click continue on that and then it goes back to the one it basically skipped. The reason I think this because it never lets me click ok it just shows correct for about 1-2secs and closes it.

    I'm not even sure the above can be a problem but is there a way for me to make it wait for a user input or click on the button. If that is indeed the problem I am facing.

    Again thank you in advance.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    You should put the code to present the next alert in a delegate method of the first alert.
     
  8. Jayman29 thread starter macrumors newbie

    Joined:
    Nov 29, 2010
    #8
    I think making a delegate would be a great idea but the problem is that I have no idea where to start. I am new at this and I am looking at forums and such but can't find one I completely understand.

    I put this at the end of the code and it doesn't seem to work.
    Code:
    - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    			
    			if (buttonIndex == 0)
    			{
    				NSLog (@"Ok");
    				
    				
    			}
    	myRandomNumber = arc4random() % 4;
    				 if (myRandomNumber == 0){
    					UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select the Nose." delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    					[playerAtt show];
    				}		
    				else if (myRandomNumber == 1){
    					UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select an Eye" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    					[playerAtt show];
    				}
    				else if (myRandomNumber == 2){
    					UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select an Ear" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    					[playerAtt show];
    				}
    				else if (myRandomNumber == 3){
    					UIAlertView *playerAtt = [[UIAlertView alloc] initWithTitle: @"Part Generated" message: @"Please Select the Mouth" delegate: nil cancelButtonTitle: @"Continue" otherButtonTitles: nil];
    					[playerAtt show];
    				}
    			
    	
    }
    
    With that change I got rid of the random part generated in each button. No errors appear but the program only says correct and nothing else now.

    I put this in my header :
    Code:
    @interface MyViewController : UIViewController <UIAlertViewDelegate> {
    
    Do I need to add tags because a lot of people on other forums speak of tags but I have no idea what they are so if I need them can someone example it.

    Thanks again.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Doesn't work how? Please be specific. Is the code being called? If not, do you have the delegate set for your first alert view? Or if so, how far into the code does it get?
     
  10. Jayman29, Dec 1, 2010
    Last edited by a moderator: Dec 1, 2010

    Jayman29 thread starter macrumors newbie

    Joined:
    Nov 29, 2010
    #10
    The app starts up it randomly generates a part. I choose the correct one and it says correct. I click the OK and it goes away. The next part never gets generated so I believe I have not delegated correctly considering it does not recognize the fact that I click OK.

    EDIT:
    Fixed!!!! Thank you for all the help!!!!
    Special thanks to dejo!!!
     

Share This Page