上次我问如何按钮时的如何添加一个值时按下的按钮
-(IBAction)addTap:(id)sender;
现在我学会了使用tapCount++;
方法内压增加一个值(tapCount是一个int类型的变量)在每次按下按钮时添加1。
但是,无论我按多少次,该值都保持不变。
我想让tapCount为1,如果我按下按钮一次,如果我按下按钮两次,则使其为2,依此类推。
有人能告诉我如何做到这一点?
详细信息:
可以说我有一个名为Player类,一个成员叫INT tapCount和INT结果
当每个按下按钮时,值将被添加到tapCount,和值将在最后显示(当游戏结束时说)
现在,当我使用NSLog检查它时,值保持不变。
Player.h
@class TappingViewController;
@interface Player : NSObject {
NSString *name;
int tapCount;
int result;
}
@property (nonatomic, assign) NSString *name;
@property (nonatomic, assign) int tapCount;
@property (nonatomic, assign) int result;
@end
TappingViewController.h
@interface TappingViewController : UIViewController {
}
-(IBAction)addTap:(id)sender;
@end
TappIngViewController.m
#import "TappingViewController.h"
#import "Player.h"
@class Player;
int tapCount;
@implementation TappingViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
/*
- (void)loadView {
}
*/
- (void)viewDidLoad
{
Player *aPlayer = [[Player alloc]init];
NSLog(@"tapCount:%d", aPlayer.tapCount);
[super viewDidLoad];
}
-(IBAction)addTap:(id)sender;
{
NSLog(@"BeforeL %d", tapCount);
tapCount++;
NSLog(@"After: %d", tapCount);
}
/*
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
@end
你能否让它更清楚。如果我是对的,那么每次按下按钮时都要计算按钮上的次数,但这取决于您要计算此计数的时间。 – saadnib 2011-03-27 11:29:40
“价值保持不变”是什么意思?你想要更新屏幕上的东西吗?你怎么确定它“保持不变”? – Mat 2011-03-27 11:30:52
更新,thx提醒 – mikemike 2011-03-27 12:27:09