无法识别的选择器错误但是选择器不存在

时间:2013-08-08 16:52:14

标签: objective-c uistoryboard iboutlet unrecognized-selector

我遇到一个问题,当我在模拟器中运行我的应用程序时出现此错误:

-[ViewController verify:]: unrecognized selector sent to instance 0x75ca600
2013-08-08 09:31:14.890 Mathy[32061:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController verify:]: unrecognized selector sent to instance 0x75ca600'

但问题是它引用的选择器甚至不存在于程序中。这是我的代码:

@interface ViewController : UIViewController
{
    int number1;
    int number2;
    float result;
}
@property (strong, nonatomic) IBOutlet UILabel *questionType;
@property (strong, nonatomic) IBOutlet UILabel *number1Label;
@property (strong, nonatomic) IBOutlet UILabel *number2Label;
@property (strong, nonatomic) IBOutlet
UILabel *op;
@property (strong, nonatomic) UITextField *userInput;
@property (strong, nonatomic) IBOutlet UILabel *CorrectOrWrong;
-(IBAction)generateQuestion:(id)sender;
@end



@implementation ViewController
@synthesize questionType;
@synthesize number1Label;
@synthesize number2Label;
@synthesize op;
@synthesize userInput;
@synthesize CorrectOrWrong;
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    self.CorrectOrWrong.hidden = YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)generateQuestion:(id)sender
{
    self.CorrectOrWrong.hidden = YES;
    self.questionType.text = @"";
    self.number1Label.text = @"";
    self.number2Label.text = @"";
    self.op.text = @"";
    self.userInput.text = @"";

    number1 = arc4random_uniform(21);
    number2 = arc4random_uniform(21);
    int operation = arc4random_uniform(4);
    switch (operation) {
        case 0:
            result = number1+number2;
            self.questionType.text = @"Addition";
            self.number1Label.text = [NSString stringWithFormat:@"%d", number1];
            self.number2Label.text = [NSString stringWithFormat:@"%d", number2];
            self.op.text = @"+";
            break;
        case 1:
            result = number1-number2;
            self.questionType.text = @"Subtraction";
            self.number1Label.text = [NSString stringWithFormat:@"%d", number1];
            self.number2Label.text = [NSString stringWithFormat:@"%d", number2];
            self.op.text = @"-";
            break;
        case 2:
            result = number1*number2;
            self.questionType.text = @"Multiplication";
            self.number1Label.text = [NSString stringWithFormat:@"%d", number1];
            self.number2Label.text = [NSString stringWithFormat:@"%d", number2];
            self.op.text = @"*";
            break;
        case 3:
            result = number1/number2;
            self.questionType.text = @"Division";
            self.number1Label.text = [NSString stringWithFormat:@"%d", number1];
            self.number2Label.text = [NSString stringWithFormat:@"%d", number2];
            self.op.text = @"÷";
            break; 
        default: result=number1+number2;
            self.questionType.text = @"Addition";
            self.number1Label.text = [NSString stringWithFormat:@"%d", number1];
            self.number2Label.text = [NSString stringWithFormat:@"%d", number2];
            self.op.text = @"+";
            break;
}

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if([self.userInput.text intValue])
    {
        self.CorrectOrWrong.hidden = NO;
        self.CorrectOrWrong.text = @"Great Job!";
    }
    else
    {
        self.CorrectOrWrong.hidden = NO;
        self.CorrectOrWrong.text = @"Uh oh! Incorrect!";  
    }

    return YES;
}

@end

1 个答案:

答案 0 :(得分:1)

错误说选择器验证:已发送到ViewController的实例。在大多数情况下,这意味着该消息是针对某些其他对象的,但该对象过早地被释放,并且ViewController的实例被分配在同一地址。

Zombies工具应该有助于找到过早解除分配的对象。如果你的项目中的其他地方有一个-verify:方法,那么释放的对象可能是该类的一个实例。