2015-04-04 53 views
1

因此,我是Objective C,XCode iOS的全新人物,所以我從一個簡單的計數器應用程序開始。從另一個方法目標中更新變量c

我已經設置了基本知識(增加按鈕按下分數),但我似乎無法獲得UILabel文本更新,每個按鈕按即每次我增加currentScore它不會更新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. 
} 

回答

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

微調了一下,謝謝! – JP89 2015-04-04 14:37:46

+0

永遠誰訪問這個 - 不要使用標籤的這種情況!第二種方法似乎更合理,但「標籤」絕對不是全球變量。 – 2015-04-07 06:21:15

+0

@ hris.to感謝您的評論,但你能說我爲什麼不使用標籤嗎?如果我們不使用標籤,並且想要在按鈕中訪問此標籤,那麼標籤的外觀如何可能,或者不是全局? – 2015-04-07 06:48:55

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

啊!謝謝。唯一的問題是,當我嘗試創建'@property int currentScore'時,出現「程序中出現意外@」的錯誤。 – JP89 2015-04-04 14:22:10

相關問題