我正在制作各种各样的记分牌。玩家可以调整三个不同的值(齿轮,水平和奖励),添加时应提供总强度。这些值中的每一个当前都以整数形式输出,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
答案 0 :(得分: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];
}