2011-03-27 57 views
0

上次我问如何按钮时的如何添加一个值时按下的按钮

-(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 
+0

你能否让它更清楚。如果我是对的,那么每次按下按钮时都要计算按钮上的次数,但这取决于您要计算此计数的时间。 – saadnib 2011-03-27 11:29:40

+0

“价值保持不变”是什么意思?你想要更新屏幕上的东西吗?你怎么确定它“保持不变”? – Mat 2011-03-27 11:30:52

+0

更新,thx提醒 – mikemike 2011-03-27 12:27:09

回答

0

addTap:方法你引用的TappingViewController的tapCount这是玩家的tapCount不同,即使它们具有相同的名称,它们是不同的。所以,你需要引用aPlayertapCount属性:

aPlayer.tapCount++; 

然而aPlayer不在addTap:方法的范围。您目前唯一可以参考的地方aPlayerviewDidLoad方法中。

这是你需要改变什么:(你不需要我已经添加了评论指出的变化)

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 
+0

谢谢乔纳森。我知道每当我使用getter和setter方法时,我都应该释放它们。在这种情况下,因为我已经将Player类导入到ViewController中,我应该将它释放到Player的.m文件还是ViewController的.m文件中? – mikemike 2011-03-27 16:59:08

+0

@mikemike,你应该释放它在同一个文件,因为你放弃了它。所以在这里你应该在viewcontroller中释放Player。 – 2011-03-27 18:50:14

0

我假设IBAction为是在您的控制器。您需要将变量添加到标题中。在控制器中,例如:

鉴于您在另一个类中有抽头计数器,您的控制器需要一个指向该类的指针。

// 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++; 
} 
+0

是的,我有一个Class文件叫做播放器,我在这个文件中声明了我的tapCount。 然后我将这个文件导入到我的控制器实现文件 并调用IBAction方法来设置tapCount值, 问题是将它们分开了吗? – mikemike 2011-03-27 13:48:06

0

我真的不觉得您提供的信息足以让我们告诉您发生了什么问题。这就是说,我可能能够指出你在正确的方向。

首先我建议你试试下面的代码:

-(IBAction)addTap:(id)sender { 
    NSLog(@"Before: %d", tapCount); 
    tapCount++; 
    NSLog(@"After: %d", tapCount); 
} 

我加入他们两个只是告诉你,增加变量确实工作。

如果你得到的输出将是这样的:

Before: 0 
After: 1 
Before: 0 
After: 1 
Before: 0 
After: 1 

这意味着你设置tapCount = 0;一遍又一遍。

如果您没有得到任何输出,这意味着您的IBAction没有正确连接。

如果您得到预期的输出,但是当您“NSLog检查它”时它是相同的。这意味着你不小心再次运行tapCount = 0;

另一种可能性是您的NSLog出现问题。

如果您有任何问题随时问。

+0

嗨,我添加了两个NsLog,因为你这样做,并确定发生在我身上,顺便说一下,我的tapCount变量位于另一个类文件中,这是否也是一个问题? – mikemike 2011-03-27 13:44:05

+0

@mikemike您的示例代码没有说tapCount在另一个类中。这个班的名字是什么? – 2011-03-27 13:48:09

+0

@mikemike您可以发布所有与'tapCount'有关的代码,以便我们告诉您问题是什么? – 2011-03-27 13:57:10