Xcode - UILabel 不会更改 viewDidAppear 上的值

发布于 2024-10-11 19:04:16 字数 1368 浏览 3 评论 0原文

大家好,我正在创建一个 iPhone 应用程序。它使用 uitableview 并进入详细视图。

现在,在详细信息视图上,我有一个滚动视图,所有数据 (XML) 都可以很好地进入其中,每次都没有问题。

我的滚动视图中的 UILabels 确实使用正确的数据进行更新,但是,它们不断在彼此之上添加子视图,以不断在标签后添加标签,这一切看起来都是胡言乱语。

我已尽一切努力尝试删除子视图,例如:

for(UIView *subview in [scrollView subviews]) {
 [subview removeFromSuperview];
}

并且

[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

所以我很困惑,我不知道发生了什么。有什么帮助吗?我一直在谷歌上搜索年龄和年龄,但不断找到不起作用的相同答案:(

任何可能对您有所帮助的网站的方向将不胜感激。

谢谢!

这是我控制器中的内容加载所有滚动视图和标签:

- (void)viewDidAppear:(BOOL)animated {

 // Create the scroll view for this view
 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
 scrollView.contentSize = CGSizeMake(320, 270);
 [scrollView setFrame:CGRectMake(0, 198, 320, 170)];

 // Do the labels for the text
 UILabel *dates = [[UILabel alloc] initWithFrame:scrollView.bounds];

 dates.textColor = [UIColor whiteColor];
 dates.font = [UIFont boldSystemFontOfSize:12];
 dates.text = round_date;
 dates.tag = 1;
 [dates setBackgroundColor:[UIColor clearColor]];
 [dates setFrame:CGRectMake(10, 10, 280, 22)];
 [scrollView addSubview:dates];
 [dates release];

 // Add the content to the main scrollview
 [self.view addSubview:scrollView];
 [scrollView release];

}

Hey everyone, I have a iPhone App I am creating. It uses a uitableview and that goes to a detail view.

Now on the detail view I have a scroll view, and all the data (XML) comes into it fine, each time, no issues.

My UILabels in my scrollview do update with the correct data, but, they keep adding subviews on top of each other to keep adding label after label and it all looks mumbo jumbo.

I have tried everything to try and remove the subviews such as:

for(UIView *subview in [scrollView subviews]) {
 [subview removeFromSuperview];
}

AND

[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

So I am stumped, I got no idea what is happening. Any help? I have been searching Google for ages and agesm, but keep finding the same answers that don't work :(

Any direction to sites that might help that you know of will be greatly appreciated.

Thanks!

Here is what I have in my controller that loads all the scrollview and labels:

- (void)viewDidAppear:(BOOL)animated {

 // Create the scroll view for this view
 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
 scrollView.contentSize = CGSizeMake(320, 270);
 [scrollView setFrame:CGRectMake(0, 198, 320, 170)];

 // Do the labels for the text
 UILabel *dates = [[UILabel alloc] initWithFrame:scrollView.bounds];

 dates.textColor = [UIColor whiteColor];
 dates.font = [UIFont boldSystemFontOfSize:12];
 dates.text = round_date;
 dates.tag = 1;
 [dates setBackgroundColor:[UIColor clearColor]];
 [dates setFrame:CGRectMake(10, 10, 280, 22)];
 [scrollView addSubview:dates];
 [dates release];

 // Add the content to the main scrollview
 [self.view addSubview:scrollView];
 [scrollView release];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我爱人 2024-10-18 19:04:16

好的,原因是每次呈现视图时 viewDidAppear 都会被调用。

如果你这样做的话

-(void) viewDidLoad;

为了确保稍后可以访问它们,您可能需要在 UIViewController 中保留对它们的引用

-(void) viewDidLoad {
    scrollView = [[UIScrollView alloc] initWithFrame];
    [self.view addSubview:scrollView];
}

,并使用以下命令在头文件中定义它:

UIScrollView *scrollView;

Ok, the reason for this is that viewDidAppear will get called every time you present the view.

If you do this is

-(void) viewDidLoad;

instead.

To make sure you can access them later you might want to keep a reference to them in your UIViewController

-(void) viewDidLoad {
    scrollView = [[UIScrollView alloc] initWithFrame];
    [self.view addSubview:scrollView];
}

and define it in your header file with:

UIScrollView *scrollView;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文