8

I'm learning to build iPhone apps with XCode 4.5.2 and I noticed something strange. As you can see at the address https://i.stack.imgur.com/purI8.jpg the text inside one of the buttons is not displayed in the iOS6 simulator. I also tried to move the Enter button in the same line of 0 and -, but the text in all three buttons of the line disappeared. Anyone knows what's the cause of this problem and how to solve it? Here is the code:

#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
Soel
  • 343
  • 2
  • 8
  • Yes, I didn't put it because the button text is not set through code, but if you need it here's the code of the view. The action related to the button is enterPressed. – Soel Nov 30 '12 at 16:24
  • 2
    Yes, I only needed to disable autolayout. – Soel Feb 03 '13 at 11:48

1 Answers1

0

According to 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

To set your button titles.

So in your case:

- (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];

}

user363349
  • 1,228
  • 14
  • 16