【问题标题】:The calculator I am making using iOS cannot print out the correct number我使用 iOS 制作的计算器无法打印出正确的数字
【发布时间】:2013-05-01 18:58:59
【问题描述】:

我正在使用 Xcode iOS SDK 6.1 制作计算器。但是,我遇到了一个问题,因为我的计算器无法将这两个数字相加。计算器似乎总是给我第一个数字作为答案。有没有办法解决这个问题?

代码如下:

#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController
@synthesize FirstNumber,SecondNumber;

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

}

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

- (void)dealloc
{
    [FirstNumber release];
    [SecondNumber release];

    [super dealloc];
}

- (IBAction)Calculate:(id)sender
{
    NSString *finalNumberString;
    int firstNumberInt, secondNumberInt, finalNumberInt;

    firstNumberInt = [FirstNumber.text integerValue];
    secondNumberInt = [SecondNumber.text integerValue];

    finalNumberInt = firstNumberInt + secondNumberInt;


    UIAlertView *alerting = [[UIAlertView alloc]initWithTitle:@"Answer" message:[NSString stringWithFormat:@"The answer is %d",finalNumberInt] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];

    [alerting show];

    _FinalNumber.text = [[NSString alloc] initWithFormat:@"%i",finalNumberInt];

}

- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
    [FirstNumber resignFirstResponder];
    [SecondNumber resignFirstResponder];
    return NO;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if ([touch view] == self.view)
    {
        [FirstNumber resignFirstResponder];
        [SecondNumber resignFirstResponder];

    }
}
@end

【问题讨论】:

  • 您是否添加了任何日志记录来跟踪进程或调试?
  • 我同意@DrummerB 的回答,但作为旁注,您应该遵循使用小写首字母命名属性和实例变量的约定。因此,您不应使用 FirstNumber_FirstNumber,而应使用 firstNumber_firstNumber。类名应使用大写字符串。

标签: ios objective-c calculator


【解决方案1】:

您似乎在警报中显示了错误的值。

[NSString stringWithFormat:@"The answer is %d",secondNumberInt]

这应该是

[NSString stringWithFormat:@"The answer is %d", finalNumberInt]

改为。

【讨论】:

  • 我用最终数字试了一下,但答案与第一个数字相同
  • interface ViewController : UIViewController 属性(保留,非原子) IBOutlet UITextField *FirstNumber;属性(保留,非原子) IBOutlet UITextField *SecondNumber;属性(保留,非原子) IBOutlet UILabel *FinalNumber;请原谅我的 at 符号,因为 stackoverflow 不允许我输入 - (IBAction)Calculate:(id)sender;
【解决方案2】:
UIAlertView *alerting = [[UIAlertView alloc]initWithTitle:@"Answer" message:[NSString stringWithFormat:@"The answer is %d",secondNumberInt] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];

secondNumberInt 看起来应该是 finalNumberInt

【讨论】:

    【解决方案3】:

    用于发出警报消息的值不正确,

        UIAlertView *alerting = [[UIAlertView alloc]initWithTitle:@"Answer" message:[NSString stringWithFormat:@"The answer is %d",secondNumberInt] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
        [alerting show];
    

    像这样改变那部分,

        UIAlertView *alerting = [[UIAlertView alloc]initWithTitle:@"Answer" message:[NSString stringWithFormat:@"The answer is %d",finalNumberInt] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
        [alerting show];
    

    【讨论】:

      【解决方案4】:

      尝试在两个整数转换之间放置一个 NSLog,并检查字符串是否可以正常读取,或者其中一个是否为零。如果其中一个为零,则可能您尚未将插座连接到界面生成器中的 UITextField,或者您只是在文本字段中输入了一些奇怪的字符串:因为您正在转换为整数,所以您不应该输入十进制数字.

      firstNumberInt = [FirstNumber.text integerValue];
      secondNumberInt = [SecondNumber.text integerValue];
      
      NSLog(@"FirstNumber string %@ integer %d", FirstNumber.text, firstNumberInt);
      NSLog(@"SecondNumber string %@ integer %d", SecondNumber.text, secondNumberInt);
      
      finalNumberInt = firstNumberInt + secondNumberInt;
      

      【讨论】:

      • 谢谢。我想我没有正确地将 UITextField 连接到 ViewController.h 。我通过重新连接来修复它。
      • 不客气。请把我的答案标记为正确的,如果你想投票的话。
      • 对不起@DrummerB,当你添加你的评论时,我正在写我的答案,我不想窃取你的答案:)
      猜你喜欢
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2020-03-16
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多