Ios 从另一个方法目标c中更新变量
我是Objective C、XCode iOS的新手,所以我从一个简单的计数器应用开始 我已经设置了增加按钮按下时的分数的基本设置,但我似乎无法在每次按下按钮时更新UILabel文本,即每次增加currentScore时,它都不会更新UILabel文本中的currentScore变量Ios 从另一个方法目标c中更新变量,ios,objective-c,xcode,Ios,Objective C,Xcode,我是Objective C、XCode iOS的新手,所以我从一个简单的计数器应用开始 我已经设置了增加按钮按下时的分数的基本设置,但我似乎无法在每次按下按钮时更新UILabel文本,即每次增加currentScore时,它都不会更新UILabel文本中的currentScore变量 您应该创建一个@property类变量 @property int currentScore @implementation JPViewController - (void)viewDidLoad {
您应该创建一个@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.
}
在你们的按钮旁边用标签做以下事情 我给你的标签贴上标签
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.
}
啊!!谢谢唯一的问题是,当我尝试创建@property int currentscoreforever时,我在程序中遇到了一个意外的错误@,请不要在这种情况下使用标记!第二种方法似乎更符合逻辑,但无论如何,“标签”绝对不是一个全局变量。@hris.to感谢您的评论,但您能告诉我为什么不使用标签吗?如果我们不使用标签,并且想在按钮中访问此标签,那么如何可能不使用外标签,或者不使其全局化?因为您正在搜索当前视图及其所有子视图,直到您找到可以动态搜索的标签。这里的大问题甚至不是性能,而是代码的可读性,因为稍后会有人检查您的所有标记,并知道每个标记代表什么。通过声明@interface JPViewController{UILabel*label;},您实际上并没有使其成为全局的。它在classI之外是不可访问的,我不会进入KVO讨论,因为KVO是高度objc特定的,这样你的类更面向对象。好的,我理解你的观点,但for循环需要更多的时间,然后标记,这就是为什么我建议使用标记,如果我们构建标记的枚举,那么任何人都会使用它而不做其他任何事情thing@hris.to我想是背景标签到标签更好,易于实现。而且速度也更快。那个么你们怎么能说使用标签是不好的呢。
- (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.
}