【问题标题】:Adding integers and displaying in a UILabel添加整数并在 UILabel 中显示
【发布时间】:2013-09-13 14:19:30
【问题描述】:

我正在制作各种记分牌。玩家可以调整三个不同的值(装备、等级和奖励),添加这些值后应提供总强度。这些值中的每一个当前都作为整数输出,并且 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

【问题讨论】:

  • 旁注 - 为什么在 .h 文件中为 levelCountgearCount 等声明全局变量?为什么不是这些私有 ivars 而不是全局变量?
  • 老实说,因为我不知道自己在做什么。我不指望你教我一切,但如果你愿意,我不介意知道其中的区别。我一直在努力学习 Objective C,但书籍和其他东西已经过时了。
  • 从一本关于 Objective-C 编程语言的好书或教程开始。 Stephen G. Kochan 的书非常好。编写应用程序首先需要了解语言。
  • 非常好。谢谢!

标签: ios objective-c int uilabel counter


【解决方案1】:

创建某种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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多