これはXCode 4 の部分的な再投稿のようです: 一部のボタンのタイトルは iOS シミュレーションで表示されませんが、質問には回答がありませんでした。
優しくしてください、私はこれに非常に慣れていません。
スタンフォードレクチャーシリーズに従ってスタックベースの電卓プログラムを実装しましたが、シミュレーターでボタンの一部が空または半分切り捨てられて表示されます。シムを実行するたびに同じボタンでそれが行われますが、ボタンを移動すると、影響を受けるボタンが変わります。通常は一番下です。
ここの最初の例: http://i.imgur.com/YpC1f.png - ボタンの一番下の行が正しく表示されない方法を参照してください。これらのボタンのいずれかを高くすると、タイトルなしで表示されますが、他の 4 つのボタンはすべて正しく表示されます。
底に近すぎるか、それらのボタンが壊れているか、または同様のものであると思いました。そこで、一番下の行全体を削除し、すべてを小さくしてから、それらのボタンを再作成したところ、タイトルが空白で 2 つが切り捨てられた 4 つのボタンが表示されます: 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