如何在按下按钮时添加值

时间:2011-03-27 11:18:00

标签: iphone objective-c ios uibutton

上次我在

的方法中按下按钮时询问如何添加值
-(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

3 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分: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

出现了问题

如果您有任何问题,请随时提出。

答案 2 :(得分:0)

addTap:方法中,您引用的是TappingViewController的tapCount,它与播放器的tapCount不同,即使它们具有相同的名称,它们也是不同的。因此,您需要引用aPlayer的{​​{1}}属性:

tapCount

但是aPlayer.tapCount++; 不在aPlayer方法的范围内。您当前可以引用addTap:的唯一地方是aPlayer方法。

这是您需要更改的内容:(您不需要我添加的评论来指出更改)

TappingViewController.h

viewDidLoad

TappIngViewController.m

@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