В прошлый раз я спросил, как сложить значение при нажатии кнопки в методе
-(IBAction)addTap:(id)sender;
теперь меня научили использовать tapCount++; (tapCount - это переменная типа int) для добавления 1 при каждом нажатии кнопки.
Однако я обнаружил, что значение оставалось неизменным независимо от того, сколько раз я нажимал на него.
Я хочу сделать значение tapCount равным 1, если я нажму кнопку один раз, и сделать его равным 2, если я нажму кнопку дважды, и так далее.
Может кто подскажет, как это сделать?
Деталь:
Допустим, у меня есть класс Player, член с именем int tapCount и int result
при каждом нажатии кнопки значение будет добавлено к 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
addTap:? В вашемaddTap:методе есть толькоtapCount++или что-то вродеplayer.tapCount++? - person Jonathan.   schedule 27.03.2011addTap:? - person Jonathan.   schedule 27.03.2011TappingViewControllerобъектPlayer? - person Erik B   schedule 27.03.2011