0

我正在制作各种记分牌。玩家可以调整三个不同的值(装备、等级和奖励),添加这些值后应提供总强度。这些值中的每一个当前都作为整数输出,并且 UILabel 显示其各自的整数。我不知道如何添加所有三个整数,然后将它们显示在 UILabel 上。我目前正在为 iOS 7 开发,但我不认为这对于当前支持的操作系统会有很大的不同。任何帮助是极大的赞赏。

。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 回答