菜鸟教程小白 发表于 2022-12-12 22:21:58

iphone - 当我按下计算器中的任何数字按钮时,iOS 模拟器会产生错误


                                            <p><p><em><strong>你好!</strong></em></p>

<p>我在 21.2 练习中遇到了麻烦。在那个练习中,我构建了分数计算器。我有 9 个按钮和数学 Action 。当我在 iOS 模拟器上按任何数字(1、2、3 或任何其他数字)时,它会产生错误:</p>

<pre><code>    2012-08-05 15:09:05.026 Fraction_Calculator -: unrecognized selector sent to instance 0x6843e00
2012-08-05 15:09:05.029 Fraction_Calculator *** Terminating app due to uncaught exception &#39;NSInvalidArgumentException&#39;, reason: &#39;-: unrecognized selector sent to instance 0x6843e00&#39;
*** 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
</code></pre>

<p><em>这意味着什么:</em>“<strong>无法识别的选择器发送到实例 0x6843e00</strong>”和“<strong>'-:无法识别的选择器发送到实例 0x6843e00'</强>”。</p>

<p><em>这是我的那个程序的代码:</em></p>

<p><strong>ViewController.h</strong></p>

<p><strong>代码:Objective-C</strong></p>

<pre><code>#import &lt;UIKit/UIKit.h&gt;
#import &#34;Calculator.h&#34;

@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
</code></pre>

<p><strong>ViewController.m</strong></p>

<p><strong>代码:Objective-C</strong></p>

<pre><code>#import &#34;ViewController.h&#34;

@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;

    ];
    display.text=displayString;
}

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

    ;
}

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

    op=theOp;

    switch(theOp)
    {
            case &#39;+&#39;:
                opStr=@&#34;+&#34;;
                break;
            case &#39;-&#39;:
                opStr=@&#34;-&#34;;
                break;
            case &#39;*&#39;:
                opStr=@&#34;*&#34;;
                break;
            case &#39;/&#39;:
                opStr=@&#34;/&#34;;
                break;
    }

    ;
    firstOperand=NO;
    isNumerator=YES;

    ;
    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
{
    ;
    isNumerator=NO;
    ;
    display.text=displayString;
}

-(IBAction) clickPlus
{
    ;
}

-(IBAction) clickMinus
{
    ;
}

-(IBAction) clickMultiply
{
    ;
}

-(IBAction) clickDivide
{
    ;
}

-(IBAction) clickEquals
{
    if (firstOperand==NO)
    {
      ;
      ;

      ;
      ];
      display.text=displayString;

      currentNumber=0;
      isNumerator=YES;
      firstOperand=YES;
      ;      
    }
}

-(IBAction) clickClear
{
    isNumerator=YES;
    firstOperand=YES;
    currentNumber=0;
    ;

    ;
    display.text=displayString;
}

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

    firstOperand=YES;
    isNumerator=YES;
    displayString=;
    myCalculator=[init];
}

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

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

@end
</code></pre>

<p><strong>非常感谢!</strong></p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>错误消息表示代码正在尝试在没有名为 <code>digit5</code> 的方法的对象上调用方法 <code>digit5</code>。如果这是您的按钮正在调用的方法,您需要在 ViewController 类中定义它,大概与 <code>digit0</code>..<code>digit9</code> 方法一起定义。</p>

<p>虽然有十个方法可能不是最理想的:更好的方法是使用对应于它们各自数字的标签来标记按钮,对所有数字按钮使用相同的方法,然后在调用中获取标签。</p>

<p><code>tag</code>是<code>UIView</code>的<code>NSInteger</code>属性,定义如下:</p>

<pre><code>@property(nonatomic) NSInteger tag
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 当我按下计算器中的任何数字按钮时,iOS 模拟器会产生错误,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/11815965/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/11815965/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 当我按下计算器中的任何数字按钮时,iOS 模拟器会产生错误