【发布时间】: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 文件中为
levelCount、gearCount等声明全局变量?为什么不是这些私有 ivars 而不是全局变量? -
老实说,因为我不知道自己在做什么。我不指望你教我一切,但如果你愿意,我不介意知道其中的区别。我一直在努力学习 Objective C,但书籍和其他东西已经过时了。
-
从一本关于 Objective-C 编程语言的好书或教程开始。 Stephen G. Kochan 的书非常好。编写应用程序首先需要了解语言。
-
非常好。谢谢!
标签: ios objective-c int uilabel counter