1

こんにちは!

21.2 運動で困っています。その演習では、Fraction calculator を作成します。私は 9 つのボタンと数学アクションを持っています。iOS シミュレーターで任意の数字 (1、2、3 またはその他) を押すと、エラーが発生します。

    2012-08-05 15:09:05.026 Fraction_Calculator[834:f803] -[ViewController digit5]: unrecognized selector sent to instance 0x6843e00
2012-08-05 15:09:05.029 Fraction_Calculator[834:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController digit5]: unrecognized selector sent to instance 0x6843e00'
*** First throw call stack:
(0x14b5022 0xeb5cd6 0x14b6cbd 0x141bed0 0x141bcb2 0x14b6e99 0x1b14e 0x1b0e6 0xc1ade 0xc1fa7 0xc1266 0x403c0 0x405e6 0x26dc4 0x1a634 0x139fef5 0x1489195 0x13edff2 0x13ec8da 0x13ebd84 0x13ebc9b 0x139e7d8 0x139e88a 0x18626 0x28ad 0x2815)
terminate called throwing an exception

意味:認識されないセレクターがインスタンス 0x6843e00 に送信されました」および「'-[ViewController digit5]: 認識されないセレクターがインスタンス 0x6843e00' に送信されました」。

そのプログラムの私のコードは次のとおりです。

ViewController.h

コード: Objective-C

#import <UIKit/UIKit.h>
#import "Calculator.h"

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *display;

-(void) processDigit: (int) digit;
-(void) processOp: (char) theOp;
-(void) storeFracPart;

-(IBAction) clickDigit: (UIButton *) sender;

-(IBAction) clickPlus;
-(IBAction) clickMinus;
-(IBAction) clickMultiply;
-(IBAction) clickDivide;

-(IBAction) clickOver;
-(IBAction) clickEquals;
-(IBAction) clickClear;

@end

ViewController.m

コード: Objective-C

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    char                op;
    int                 currentNumber;
    BOOL                firstOperand, isNumerator;
    Calculator          *myCalculator;
    NSMutableString     *displayString;
}

@synthesize display;

-(void) processDigit:(int)digit
{
    currentNumber=currentNumber*10+digit;

    [displayString appendString:[NSString stringWithFormat:@"%i", digit]];
    display.text=displayString;
}

-(IBAction) clickDigit:(UIButton *)sender
{
    int digit=sender.tag;

    [self processDigit: digit];
}

-(void) processOp: (char) theOp
{
    NSString *opStr;

    op=theOp;

    switch(theOp)
    {
            case '+':
                opStr=@"+";
                break;
            case '-':
                opStr=@"-";
                break;
            case '*':
                opStr=@"*";
                break;
            case '/':
                opStr=@"/";
                break;
    }

    [self storeFracPart];
    firstOperand=NO;
    isNumerator=YES;

    [displayString appendString: opStr];
    display.text=displayString;
}

-(void) storeFracPart
{
    if(firstOperand)
    {
       if(isNumerator)
       {
           myCalculator.operand1.numerator=currentNumber;
           myCalculator.operand1.denominator=1;
       }
        else
            myCalculator.operand1.denominator=currentNumber;
    }
    else if (isNumerator)
    {
        myCalculator.operand2.numerator=currentNumber;
        myCalculator.operand2.denominator=1;
    }
    else
    {
        myCalculator.operand2.denominator=currentNumber;
        firstOperand=YES;
    }
currentNumber=0;
}

-(IBAction) clickOver
{
    [self storeFracPart];
    isNumerator=NO;
    [displayString appendString: @"/"];
    display.text=displayString;
}

-(IBAction) clickPlus
{
    [self processOp: '+'];
}

-(IBAction) clickMinus
{
    [self processOp: '-'];
}

-(IBAction) clickMultiply
{
    [self processOp: '*'];
}

-(IBAction) clickDivide
{
    [self processOp: '/'];
}

-(IBAction) clickEquals
{
    if (firstOperand==NO)
    {
        [self storeFracPart];
        [myCalculator performOperation: op];

        [displayString appendString: @"="];
        [displayString appendString: [myCalculator.accumulator convertToString]];
        display.text=displayString;

        currentNumber=0;
        isNumerator=YES;
        firstOperand=YES;
        [displayString setString: @""];        
    }
}

-(IBAction) clickClear
{
    isNumerator=YES;
    firstOperand=YES;
    currentNumber=0;
    [myCalculator clear];

    [displayString setString:@""];
    display.text=displayString;
}

- (void)viewDidLoad
{
    //[super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    firstOperand=YES;
    isNumerator=YES;
    displayString=[NSMutableString stringWithCapacity:40];
    myCalculator=[[Calculator alloc]init];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

どうもありがとう!

4

2 に答える 2

1

これは、各ボタンにタグ値を設定していない場合に発生する可能性があります。たとえば、ボタン 4 の場合、タグは 4 にする必要があります。

ここに画像の説明を入力

于 2012-08-04T10:50:15.193 に答える
0

数字ボタンのタグプロパティ(Interface BuilderのUIButton設定)を確認します。それらすべての値が0のようです。ボタンのタグを適切に設定するだけです(例:数字3のボタンの場合はtag = 3)。

于 2012-08-04T10:54:19.493 に答える