在xcode崩溃后,某些按钮具有空标题或部分截断

时间:2012-10-22 01:07:17

标签: objective-c ios xcode

这似乎是XCode 4: some button titles not visible in iOS simulation的部分重新发布,但他们的问题没有得到解答。

请保持温和,我对此很陌生。

我根据stamford讲座系列实现了一个基于堆栈的计算器程序,但是我的一些按钮在模拟器中显示为空或半截断。每次运行sim时都会使用相同的按钮,但如果我移动任何按钮,它会改变哪些按钮会受到影响。它通常是最底层的。

此处的第一个示例:http://i.imgur.com/YpC1f.png - 查看底行按钮无法正确显示?如果我使这些按钮中的任何一个更高,它将显示没有标题,但所有其他四个按钮将显示正确。

我认为它可能太靠近底部,或者那些按钮坏了或类似。所以我删除了整个底行,使所有内容变小,然后重新创建了这些按钮,现在我得到了四个带有空白标题的按钮和两个被截断的按钮:http://i.imgur.com/kM1Rb.png

请注意,按钮仍然按预期工作,只是显示不正确。

我做错了吗?任何建议表示赞赏。

编辑:来自控制器的完整代码:

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

@interface CalculatorViewController()
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (nonatomic, strong) CalculatorBrain *brain;
- (void)updateStackDisplay:(NSString *)value;
@end

@implementation CalculatorViewController

@synthesize display = _display;
@synthesize stackDisplay = _stackDisplay;
@synthesize userIsInTheMiddleOfEnteringANumber = _userIsInTheMiddleOfEnteringANumber;
@synthesize brain = _brain;

- (CalculatorBrain *)brain {
    if (!_brain) _brain = [[CalculatorBrain alloc] init];
    return _brain;
}

- (void)updateStackDisplay:(NSString *)value {
    // if there's nothing sent so far, just initialise it
    if (self.stackDisplay.text.length == 0) {
        self.stackDisplay.text = value;
        return;
    }
    // This part is a little confusing. Extra assignment asked for = to be added to the end of the stack label if an operation was pressed.
    // Here I check for = at the end of the label and remove it so it's only displayed once. If "=" is being passed (done by the operation itself), it will be added back on right at the end of this function.
    if ([self.stackDisplay.text rangeOfString:@"="].location == (self.stackDisplay.text.length - 1)) {
        // .location starts at zero, .length doesn't.
        self.stackDisplay.text = [self.stackDisplay.text substringToIndex:[self.stackDisplay.text length]-1];
    }       
    // If we add a space after remove the = we'll end up with double space. Hence the else if, not if.
    else if (!self.userIsInTheMiddleOfEnteringANumber) {
        self.stackDisplay.text = [self.stackDisplay.text stringByAppendingString:@" "];
    }
    self.stackDisplay.text = [self.stackDisplay.text stringByAppendingString:value];
}

- (IBAction)digitPressed:(UIButton *)sender {
    NSString *digit = sender.currentTitle;
    if ([digit isEqualToString:@"."]) {
        if ([self.display.text rangeOfString:@"."].location != NSNotFound) {
            return;
        }
        else {
            if (!self.userIsInTheMiddleOfEnteringANumber) digit = @"0.";
        }
    }
    [self updateStackDisplay:digit];
    if (self.userIsInTheMiddleOfEnteringANumber) {
        self.display.text = [self.display.text stringByAppendingString:digit];
    }
    else {  
        self.display.text = digit;
        self.userIsInTheMiddleOfEnteringANumber = YES;
    }
}

- (IBAction)operationPressed:(UIButton *)sender {
    if (self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
    [self updateStackDisplay:sender.currentTitle];
    [self updateStackDisplay:@"="];
    double result = [self.brain performOperation:sender.currentTitle];
    NSString *resultString = [NSString stringWithFormat:@"%g", result];
    self.display.text = resultString;
}

- (IBAction)enterPressed {
    if (self.userIsInTheMiddleOfEnteringANumber) {
        self.userIsInTheMiddleOfEnteringANumber = NO;
    }
    [self.brain pushOperand:[self.display.text doubleValue]];
}

- (IBAction)clearPressed {
    self.stackDisplay.text = @"";
    [self.brain clearStack];
    self.userIsInTheMiddleOfEnteringANumber = NO;
    self.display.text = @"0";
}

@end

1 个答案:

答案 0 :(得分:0)

我得到了一个类似的错误,其中一些UIButtons有空文本,尽管通过输出titleLabel.text获得了正确的文本值。我删除了按钮,并将它们添加到故事板之前的相同位置。然后它奏效了。