1

我是 Objective C 和 Xcode 的新手,所以请耐心等待!我编写了一个控制 5 个按钮和 1 个重置按钮的程序。如果按下 5 个按钮中的任何一个,它们都应该被禁用(灰显),直到按下重置按钮。这是我的尝试:

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    IBOutlet UIButton *button1;
    IBOutlet UIButton *button2;
    IBOutlet UIButton *button3;
    IBOutlet UIButton *button4;
    IBOutlet UIButton *button5;
    IBOutlet UIButton *resetButton;
}
    @property(retain,nonatomic)UIButton *button1;
    @property(retain,nonatomic)UIButton *button2;
    @property(retain,nonatomic)UIButton *button3;
    @property(retain,nonatomic)UIButton *button4;
    @property(retain,nonatomic)UIButton *button5;
    @property(retain,nonatomic)UIButton *resetButton;
    - (IBAction)anyButtonPressed:(UIButton *)sender;
    - (IBAction)resetPressed:(UIButton *)sender;

@end

视图控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize button1, button2, button3, button4, button5, resetButton;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(IBAction)anyButtonPressed{
    button1.enabled = NO;
    button2.enabled = NO;
    button3.enabled = NO;
    button4.enabled = NO;
    button5.enabled = NO;

}

-(IBAction)resetPressed{
    button1.enabled = YES;
    button2.enabled = YES;
    button3.enabled = YES;
    button4.enabled = YES;
    button5.enabled = YES;

}

@end

但它说 ViewController 旁边的实现不完整,当我在模拟器中按下一个按钮时,它崩溃了。请帮帮我?还请提出任何更简单的方法来实现这一点!

4

1 回答 1

1

您的按钮操作方法签名在您的实现中不正确。将您的代码更改为:

-(IBAction)anyButtonPressed:(UIButton *)sender {
    button1.enabled = NO;
    button2.enabled = NO;
    button3.enabled = NO;
    button4.enabled = NO;
    button5.enabled = NO;
}

-(IBAction)resetPressed:(UIButton *)sender {
    button1.enabled = YES;
    button2.enabled = YES;
    button3.enabled = YES;
    button4.enabled = YES;
    button5.enabled = YES;
}
于 2012-11-12T23:54:38.940 回答