How to add up a value when a button pressed
Last time I have asked how to add up a value when a button is pressed within the method of
-(IBAction)addTap:(id)sender;
now I was taught to use tapCount++;
(tapCount is a int type variable) to add 1 everytime the button was pressed.
However, I find that the value stayed the same no matter how many times I pressed it.
I want to make tapCount to be 1 if I press the button once, and make it 2 if I pressed the button twice, and so on.
Can someone tell me how to do this?
Detail:
Lets say I have a class called Player开发者_如何学C, a member called int tapCount and int result
when each time the button was pressed, a value will be added to tapCount, and the value will be displayed at the end (when the game end lets say)
For now, the value stay the same when I use NSLog to check it.
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
In the addTap:
method you are referencing the TappingViewController's tapCount
which is different to the Player's tapCount
, even though they have the same name, they are different. So you need to reference the aPlayer
's tapCount
property:
aPlayer.tapCount++;
However aPlayer
isn't in the addTap:
method's scope. The only place you can currently reference aPlayer
is in the viewDidLoad
method.
This is what you need to change: (you don't need the comments I've added to point out the changes)
TappingViewController.h
@class Player; //**You have imported the Player class in the .m file so if you use the Player class in the header you need to add it as a forward class.
@interface TappingViewController : UIViewController {
Player *aPlayer; //**This is an instance variable (ivar) so you can access it in any method in the implementation (.m file), however you still need to put something in this ivar (see viewDidLoad)**
}
//**You can add a property for aPlayer if you want, but remember to do the memory management properly**
-(IBAction)addTap:(id)sender;
@end
TappIngViewController.m
#import "TappingViewController.h"
#import "Player.h"
//**Get rid of the forward class, as you have imported it above**
//**Get rid of the tapCount that was were**
@implementation TappingViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
/*
- (void)loadView {
}
*/
- (void)viewDidLoad
{
aPlayer = [[Player alloc] init]; //**remove the declaration of a new var**
NSLog(@"tapCount:%d", aPlayer.tapCount);
[super viewDidLoad];
}
-(IBAction)addTap:(id)sender;
{
NSLog(@"BeforeL %d", tapCount);
aPlayer.tapCount++; //**reference the player's tapCount**
NSLog(@"After: %d", tapCount);
}
/*
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
@end
I really don't feel like you're providing enough information to make it possible for us to tell you what's going wrong. That being said, I might be able to point you in the right direction.
First of all I would suggest that you try the following code:
-(IBAction)addTap:(id)sender {
NSLog(@"Before: %d", tapCount);
tapCount++;
NSLog(@"After: %d", tapCount);
}
I added both of them just to show you that incrementing the variable does indeed work.
If you get output that looks like this:
Before: 0
After: 1
Before: 0
After: 1
Before: 0
After: 1
It means that you set tapCount = 0;
over and over again.
If you don't get any output it means that your IBAction isn't connected properly.
If you get the expected output, but it's the same when you "NSLog to check it". It means that you've accidentally run tapCount = 0;
again.
Another possibility is that there is something wrong with your NSLog
.
If you have any questions feel free to ask.
I assuming the IBAction is in your controller. You will need to add the variable to your header. In the controller for example:
Given that you have the tap counter in another class, your controller needs a pointer to that class.
// controller header file
Player *myPlayer;
// controller implementation file
-(void)awakeFromNib {
myPlayer = [Player alloc] init]; // initialized the player
// do whatever else you need to do
// load previous data from NSUserDefaults, maybe
}
-(IBAction)addTap:(id)sender {
myPlayer.tapCount++;
}
精彩评论