0

我不知道如何在 IBAction 中创建 if-else 语句!我只能在按钮点击中制作它们......我想要它做的是检查 int 问题所在的 # 并进行适当的更改。请帮忙!

主视图.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

主视图.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
4

2 回答 2

2

我建议使用 IBAction-call 的 sender 参数。每次调用 IBAction 方法时,发出调用的对象都会随之发送,从而可以轻松确定单击了哪个按钮。

- ( IBAction ) buttonClicked:( id ) sender {

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

      questionLabel.text = @"FAIL";
      ...
   }
}
于 2009-07-14T06:40:29.550 回答
1

根据Beginning iPhone Development,您也可以将发送者与按钮的指针进行比较

-(IBAction)buttonPressed:(id) sender {
     if (sender == firstPointer ) {    
          ...
     }
     else if (sender == secondPointer ) {    
          ...
     }
     ...
 }
于 2009-12-22T19:26:45.340 回答