0

私はある種のスコアボードに取り組んでいます。プレーヤーは、3 つの異なる値 (ギア、レベル、およびボーナス) を調整できます。これらを追加すると、総合的な強さが得られます。これらの各値は現在整数として出力されており、UILabel はそれぞれの整数を表示します。3 つの整数すべてを追加して UILabel に表示する方法がわかりません。私は現在 iOS 7 向けに開発していますが、これが現在サポートされている OS と大きく異なるとは思いません。どんな助けでも大歓迎です。

.h

#import <UIKit/UIKit.h>

int levelCount;
int gearCount;
int oneShotCount;
int totalScoreCount;

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *totalScore;
@property (weak, nonatomic) IBOutlet UILabel *playerName;
@property (weak, nonatomic) IBOutlet UILabel *levelNumber;
@property (weak, nonatomic) IBOutlet UILabel *gearNumber;
@property (weak, nonatomic) IBOutlet UILabel *oneShotNumber;
- (IBAction)levelUpButton:(id)sender;
- (IBAction)levelDownButton:(id)sender;
- (IBAction)gearUpButton:(id)sender;
- (IBAction)gearDownButton:(id)sender;
- (IBAction)oneShotUpButton:(id)sender;
- (IBAction)oneShotDownButton:(id)sender;


@end

.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

    int ans = levelCount + gearCount + oneShotCount;
    self.levelNumber.text = [NSString stringWithFormat:@"%i", ans];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)levelUpButton:(id)sender {
    levelCount = levelCount + 1;
    self.levelNumber.text = [NSString stringWithFormat:@"%i", levelCount];


}

- (IBAction)levelDownButton:(id)sender {
    levelCount = levelCount - 1;
    self.levelNumber.text = [NSString stringWithFormat:@"%i", levelCount];

}


- (IBAction)gearUpButton:(id)sender {
    gearCount = gearCount + 1;
    self.gearNumber.text = [NSString stringWithFormat:@"%i", gearCount];
}

- (IBAction)gearDownButton:(id)sender {
    gearCount = gearCount - 1;
    self.gearNumber.text = [NSString stringWithFormat:@"%i", gearCount];
}


- (IBAction)oneShotUpButton:(id)sender {
    oneShotCount = oneShotCount + 1;
    self.oneShotNumber.text = [NSString stringWithFormat:@"%i", oneShotCount];
}

- (IBAction)oneShotDownButton:(id)sender {
    oneShotCount = oneShotCount - 1;
    self.oneShotNumber.text = [NSString stringWithFormat:@"%i", oneShotCount];
}



@end
4

1 に答える 1

3

updateScore他の値のいずれかが変更されるたびに呼び出される何らかのメソッドを作成します。

- (void)updateScore {
    totalScoreCount = ... // calculate score from levelCount, gearCount, and oneShotCount

    self.totalScore.text = [NSString stringWithFormat:@"%i", totalScoreCount];
}

次に、各メソッド...UpButton:...DownButton:メソッドで次を呼び出します。

[self updateScore];

最初に他の値を更新してから、必ずこれを呼び出してください。例:

- (IBAction)levelUpButton:(id)sender {
    levelCount = levelCount + 1;
    self.levelNumber.text = [NSString stringWithFormat:@"%i", levelCount];
    [self updateScore];
}
于 2013-09-10T01:15:30.493 に答える