UILabel 的背景颜色不会改变

发布于 2024-12-24 01:36:04 字数 698 浏览 0 评论 0原文

我有一个标签,我想在选择按钮时更改其背景颜色。当我第一次进入视图时它工作正常,但当我离开视图并返回时,它不再工作,即使我仍然可以通过标签访问标签并读取其文本。

UILabel *labelToFlip = (UILabel *)[self.view viewWithTag:labelTag];
if (button.selected)
{    
    NSLog(@"selected");
    NSLog(@"label text:%@:%d", labelToFlip.text, labelToFlip.tag);
    [labelToFlip setBackgroundColor:[UIColor blackColor]];
    [labelToFlip setTextColor:[UIColor whiteColor]];
}

这就是我回到我的观点。我再次可以毫无问题地访问标签并阅读文本,只是无法更改背景颜色或文本颜色。

[self.navigationController popViewControllerAnimated:YES];
[self.navigationController pushViewController:self.navigationController.parentViewController
                                     animated:YES];

I have a label that I want to change the background color of when a button is selected. It works fine when I first enter the view but when I leave the view and return, it no longer works even though I can still access the label by its tag and read its text.

UILabel *labelToFlip = (UILabel *)[self.view viewWithTag:labelTag];
if (button.selected)
{    
    NSLog(@"selected");
    NSLog(@"label text:%@:%d", labelToFlip.text, labelToFlip.tag);
    [labelToFlip setBackgroundColor:[UIColor blackColor]];
    [labelToFlip setTextColor:[UIColor whiteColor]];
}

This is how I return to my view. Again I can get to the label and read the text with no problem, just can't change the background color or text color.

[self.navigationController popViewControllerAnimated:YES];
[self.navigationController pushViewController:self.navigationController.parentViewController
                                     animated:YES];

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

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

发布评论

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

评论(1

鸠魁 2024-12-31 01:36:04

扩展您的代码,也许可以更理解我上面尝试评论的内容。

UINavigationController* navigationController = [self navigationController];

UIViewController* parentViewControllerForNavigationController = [navigationController parentViewController];

然后在代码中将父 ViewController 添加到子 ViewController。

[navigationController pushViewController:parentViewControllerForNavigationController animated:YES];

无论您在哪里执行此操作都会导致一些重大问题。我认为您不太了解 UINavigationController 的工作原理。简单的解释是,它实际上只是 UIViewController(或 UIViewController 的子类)列表(或队列,如果您愿意)的迭代器。实际上,将父视图控制器添加到导航控制器的方式会导致导航控制器的复制。

代码:

[self.navigationController popViewControllerAnimated:YES];

简单地从列表中删除最后一项,它将调用位于 UINavigationController 堆栈末尾的 UIViewController 上的 release 和 dealloc 方法。这将导致应用程序以动画方式返回到前一个视图(因为 animate 参数为“YES”),您根本不需要将另一个视图推到导航堆栈上,这似乎是您想要做的。

但是,如果您尝试执行一些导航操作,例如:
您有三个视图控制器:UIViewController A、B 和 C。
根视图控制器是 A。您将 B 推到导航控制器上,然后在 B 中执行某些功能,并希望将视图控制器 C 推到导航堆栈上,但是当您在视图 C 中按回时,您希望看到视图 A而不是视图 B,您可以通过调用 [self navigationController] viewControllers] 并删除倒数第二个对象并调整数组大小,然后调用 [self navigationController] setViewControllers:"resizedArray"] 来操作返回的数组,但我会让此功能发生在viewDidAppear 函数在视图控制器 C 的实现中。

To expand out your code and perhaps make more sense of what I have tried to comment about above.

UINavigationController* navigationController = [self navigationController];

UIViewController* parentViewControllerForNavigationController = [navigationController parentViewController];

Then in your code you are then adding the parent ViewController to the child ViewController.

[navigationController pushViewController:parentViewControllerForNavigationController animated:YES];

Which no matter where you perform this is going to cause some major problems. I think you aren't quite understanding how a UINavigationController works. The simple explanation is that it's really just an iterator for an list (or a queue if you prefer) of UIViewControllers (or subclasses of UIViewControllers). In effect the way you are adding the parent view controller to the navigation controller is causing a replication of the navigation controller.

The code:

[self.navigationController popViewControllerAnimated:YES];

Simply removes the last item from the list, it will call the release and dealloc methods on the UIViewController that is at the end of the UINavigationController stack. Which will cause the App to animate back to the previous view (as the animate argument is 'YES'), you do not need to push another view onto the navigation stack here at all, which is what it seems like you are trying to do.

However if you were for example trying to perform some navigation manipulation such as:
You have three view controllers, UIViewController A,B and C.
The Root View Controller is A. You push B onto the navigation controller then perform some function in B and want view controller C to be pushed onto the navigation stack, but when you press back in view C you would like to be presented with View A and not View B you could manipulate the array returned by calling [self navigationController] viewControllers] and removing the second last object and resizing the array and then calling [self navigationController] setViewControllers:"resizedArray"] but I would have this functionality happen in the viewDidAppear function in view controller C's implementation.

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