从另一个方法目标中更新变量c

时间:2015-04-04 14:13:28

标签: ios objective-c xcode

所以我是Objective C,XCode iOS的新手,所以我开始使用一个简单的计数器应用程序。

我已经设置了基本设置(按下按钮增加分数)但我似乎无法在每个按钮按下时更新UILabel文本,即每次我增加currentScore它没有' t更新currentScore

中的UILabel text变量

任何帮助将不胜感激!

#import "JPViewController.h"

@implementation JPViewController

int currentScore;

- (void)viewDidLoad
{

    [super viewDidLoad];

    //Init currentScore
    currentScore = 0;
    NSLog(@"Your score is %d", currentScore);

    //Points text label

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 290, 400)];
    label.text = [NSString stringWithFormat:@"Your score is: %d", currentScore];
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    //TODO: Position points in centre.
    [self.view addSubview:label];

    //Add Points Button

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"Press Me" forState:UIControlStateNormal];
    [button sizeToFit];
    button.center = CGPointMake(320/2, 60);
    [button addTarget:self action:@selector(buttonPressed:)
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

- (void)buttonPressed:(UIButton *)button {
    NSLog(@"Button Pressed");
    currentScore++;
    NSLog(@"Your score is %d", currentScore);
    //TODO: Update the label with the new currentScore here.
}

2 个答案:

答案 0 :(得分:-1)

你应该创建一个@property(类变量)

@property int currentScore

@implementation JPViewController

- (void)viewDidLoad
{

    [super viewDidLoad];

    //Init currentScore
    self.currentScore = 0;
    NSLog(@"Your score is %d", self.currentScore);

    //Points text label

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 290, 400)];
    label.text = [NSString stringWithFormat:@"Your score is: %d", self.currentScore];
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    //TODO: Position points in centre.
    [self.view addSubview:label];

    //Add Points Button

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"Press Me" forState:UIControlStateNormal];
    [button sizeToFit];
    button.center = CGPointMake(320/2, 60);
    [button addTarget:self action:@selector(buttonPressed:)
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

- (void)buttonPressed:(UIButton *)button {
    NSLog(@"Button Pressed");
    self.currentScore++;
    NSLog(@"Your score is %d", self.currentScore);
    //TODO: Update the label with the new currentScore here.
}

答案 1 :(得分:-1)

要在按钮旁边使用标签,请执行以下操作

1)为您的标签添加标签

 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 290, 400)];
    label.text = [NSString stringWithFormat:@"Your score is: %d", currentScore];
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    //TODO: Position points in centre.
    [label setTag:1000];//Give tag to your label 
    [self.view addSubview:label];

2)然后在按钮方法

中通过它的标签获得标签
- (void)buttonPressed:(UIButton *)button {
    NSLog(@"Button Pressed");
    currentScore++;
    NSLog(@"Your score is %d", currentScore);
    UILabel *label = (UILabel) [self.view viewWithTag:1000]; // you get your label reference here
    [label setText:[NSString stringWithFormate:%d,currentScore]]; // then store your current score in your lable
    //TODO: Update the label with the new currentScore here.
}

或者这是第二种方法

1)使你的UILabel变量全局如下

 @implementation JPViewController
    {
          UILabel *label;
    }

2)在viewDidLoad()

label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 290, 400)];
        label.text = [NSString stringWithFormat:@"Your score is: %d", currentScore];
        [label setFont:[UIFont boldSystemFontOfSize:16]];
        //TODO: Position points in centre.
        [self.view addSubview:label];

3)在你的按钮事件中

- (void)buttonPressed:(UIButton *)button {
        NSLog(@"Button Pressed");
        currentScore++;
        NSLog(@"Your score is %d", currentScore);
        [label setText:[NSString stringWithFormate:%d,currentScore]]; // then store your current score in your lable
        //TODO: Update the label with the new currentScore here.
    }