我正在开发一个包含以下代码的 iOS 5 计算器:
- (IBAction)digitPressed:(UIButton *)sender
{
//Enter digit, avoiding two-decimal-point case.
NSString *digit = sender.currentTitle;
NSRange range = [self.display.text rangeOfString:@"."];
if (range.location==NSNotFound || (![digit isEqualToString:@"."]))
if (self.userIsEnteringNumber)
{
self.display.text = [self.display.text stringByAppendingString:digit];
}
else
{
if (![sender.currentTitle isEqualToString:@"."])
{
self.display.text = digit;
}
else
{
self.display.text = @"0.";
}
self.userIsEnteringNumber = YES;
}
}
和
- (IBAction)changeSignPressed
{
//Reverse sign from positive to negative or vice versa.
if (self.numberIsNegative) self.numberIsNegative = NO;
if (!(self.numberIsNegative)) self.numberIsNegative = YES;
//Correct display to reflect change in sign.
if (self.numberIsNegative)
{
if (![[self.display.text substringToIndex:1] isEqualToString:@"-"])
{
self.display.text = [@"-" stringByAppendingString:self.display.text];
}
}
if (!self.numberIsNegative)
{
/*This isn't working--why not?
if ([[self.display.text substringToIndex:1] isEqualToString:@"-"])
{
NSLog(@"Okay."); //This doesn't log, so obv. the problem is in the previous line.
self.display.text = [self.display.text substringFromIndex:1];
}
*/
}
}
无论我如何摆弄它,我都无法让 changeSignPressed 中的注释掉的代码工作。关于如何解决这个问题的任何想法?