【问题标题】:Simulator does not show button text模拟器不显示按钮文本
【发布时间】:2012-11-18 19:46:44
【问题描述】:

我正在学习使用 XCode 4.5.2 构建 iPhone 应用程序,但我发现了一些奇怪的东西。正如您在地址http://i.stack.imgur.com/purI8.jpg 看到的那样,其中一个按钮内的文本不会显示在iOS6 模拟器中。我还尝试将 Enter 按钮移到 0 和 - 的同一行中,但是该行的所有三个按钮中的文本都消失了。任何人都知道这个问题的原因是什么以及如何解决它?代码如下:

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

@interface CalculatorViewController()
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (nonatomic, strong) CalculatorBrain *brain;
@end

@implementation CalculatorViewController

@synthesize display;
@synthesize userIsInTheMiddleOfEnteringANumber;
@synthesize brain = _brain;

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

- (IBAction)digitPressed:(UIButton *)sender
{    
    NSString *digit = [sender currentTitle];
    if (self.userIsInTheMiddleOfEnteringANumber) {
        self.display.text = [self.display.text stringByAppendingString:digit];
    } else {
        self.display.text = digit;
        self.userIsInTheMiddleOfEnteringANumber = YES;
    }
}

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

- (IBAction)operationPressed:(UIButton *)sender
{
    if (self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];

    NSString *operation = [sender currentTitle];
    double result = [self.brain performOperation:operation];
    self.display.text = [NSString stringWithFormat:@"%g", result];
}

@end

【问题讨论】:

  • 是的,我没有放,因为按钮文本不是通过代码设置的,但是如果你需要,这里是视图的代码。与按钮相关的操作为 enterPressed。
  • 是的,我只需要禁用自动布局。

标签: button ios6 ios-simulator xcode4.5


【解决方案1】:

根据https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/doc/uid/TP40006815-CH3-SW7

- (void)setTitle:(NSString *)title forState:(UIControlState)state

设置按钮标题。

所以在你的情况下:

- (IBAction)operationPressed:(UIButton *)sender{
   ....
   [sender setTitle:[NSString stringWithFormat:@"%g", result] forState: UIControlStateNormal];

   // lets assume you want the down states as well:
   [sender setTitle:[NSString stringWithFormat:@"%g", result] forState: UIControlStateSelected];
   [sender setTitle:[NSString stringWithFormat:@"%g", result] forState: UIControlStateHighlighted];

}

【讨论】:

    猜你喜欢
    • 2014-08-27
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2013-01-11
    • 1970-01-01
    相关资源
    最近更新 更多