ios 使用 viewdidappear 更新程序化 uilabels 的正确方法
每次加载此选项卡而不是更新动态标签时,它都会在其上写入一个全新的标签。我需要在这段代码中放入什么,以便它更新它或清除动态标签,然后放入新标签。我觉得这可能只是一个简单的一行修复。这是代码的简化版本:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
goalArray = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"http://localhost/goal.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:test forKey:@"name"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIFormDataRequest *)request
{
...
for (id myArrayElement in goalArray)
{
NSLog(@"y value:%i", yValue);
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, yValue, 80, 44)];
label.font = [UIFont systemFontOfSize:12];
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.text = myArrayElement;
[self.view addSubview:label];
yValue += 44;
}
}
Every time this tab get loaded instead of updating the dynamic label, it writes a brand new label over it. What do I need to put in this code so that it updates it or clears the dynamic labels and then put in the new label. I feel like its probably just a simple one line fix. Here is a simplified version of the code:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
goalArray = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"http://localhost/goal.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:test forKey:@"name"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIFormDataRequest *)request
{
...
for (id myArrayElement in goalArray)
{
NSLog(@"y value:%i", yValue);
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, yValue, 80, 44)];
label.font = [UIFont systemFontOfSize:12];
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.text = myArrayElement;
[self.view addSubview:label];
yValue += 44;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 requestFinished: 的
for
循环之前,添加以下内容:然后在
for
循环内添加以下内容:In requestFinished: before the
for
loop, add this:and then inside your
for
loop, add this: