views:

442

answers:

2

I don't know how to make an if-else statement within an IBAction! I can only make them in the button clicks... what I want it to do is check for which # the int question is at and make the appropriate changes. please help!

MainView.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MainView : UIView{
    IBOutlet UIImageView *background;
    IBOutlet UIButton *button1;
    IBOutlet UIButton *button2;
    IBOutlet UIButton *button3;
    IBOutlet UIButton *button4;
    IBOutlet UILabel *questionLabel;
}

@property (nonatomic, retain) UIButton *button1;
@property (nonatomic, retain) UIButton *button2;
@property (nonatomic, retain) UIButton *button3;
@property (nonatomic, retain) UIButton *button4;
@property (nonatomic, retain) UILabel *questionLabel;
@property (nonatomic, retain) UIImageView *background;

- (IBAction)Button1clicked;
- (IBAction)Button2clicked;
- (IBAction)Button3clicked;
- (IBAction)Button4clicked;
@end

MainView.m

    #import "MainView.h"

    @implementation MainView

    @synthesize button1, button2, button3, button4, questionLabel, background;

    int question = 1;

    BOOL oneRight = FALSE;
    BOOL twoRight = FALSE;
    BOOL threeRight = FALSE;
    BOOL fourRight = FALSE;


    - (IBAction)Button1clicked {
        if (oneRight == TRUE) {
         questionLabel.text = @"CORRECT!";
         question++;
        } else {
         questionLabel.text = @"FAIL"; 
        }
    }

    - (IBAction)Button2clicked {
        if (twoRight == TRUE) {
         questionLabel.text = @"CORRECT!";
         question++;
        } else {
         questionLabel.text = @"FAIL"; 
        }
    }

    - (IBAction)Button3clicked {
        if (threeRight == TRUE) {
         questionLabel.text = @"CORRECT!";
         question++;
        } else {
         questionLabel.text = @"FAIL"; 
        }
    }

    - (IBAction)Button4clicked {
        if (fourRight == TRUE) {
         questionLabel.text = @"CORRECT!";
         question++;
        } else {
         questionLabel.text = @"FAIL"; 
        }
    }

    int foo(int question) {
        if (question == 1) {
        oneRight = TRUE;
        twoRight = FALSE;
        threeRight = FALSE;
        fourRight = FALSE;
        }
    }

    @end
+1  A: 

I would suggest making use of the sender parameter of the IBAction-call. Everytime a IBAction-method is called the object making the call is sent along with it making it easy to determine which button was clicked.

- ( IBAction ) buttonClicked:( id ) sender {

   if( [ sender tag ] == answer ) {
      questionLabel.text = @"CORRECT";
      ...
   } 
   else {

      questionLabel.text = @"FAIL";
      ...
   }
}
suraken
ill do that thanks! but my question is that i want to be always checking if(question == 1){ answer = 1; } else if(question == 2) { answer = 2; }so basically i want it to always be checking which question it is!
+1  A: 

According to Beginning iPhone Development, you can also just compare the sender to the button's pointer

-(IBAction)buttonPressed:(id) sender {
     if (sender == firstPointer ) {    
          ...
     }
     else if (sender == secondPointer ) {    
          ...
     }
     ...
 }
Frank
I actually just got this book and am reading now! I just discovered that!