因此,我是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.
}
微調了一下,謝謝! – JP89 2015-04-04 14:37:46
永遠誰訪問這個 - 不要使用標籤的這種情況!第二種方法似乎更合理,但「標籤」絕對不是全球變量。 – 2015-04-07 06:21:15
@ hris.to感謝您的評論,但你能說我爲什麼不使用標籤嗎?如果我們不使用標籤,並且想要在按鈕中訪問此標籤,那麼標籤的外觀如何可能,或者不是全局? – 2015-04-07 06:48:55