简体   繁体   English

如何从另一个ViewController更改背景颜色?

[英]How to change the background color from another ViewController?

I have two view controllers, the Home and the other that is showed by a cocoacontrol animation (MJPopupViewController) and idn the one that is showed when you push a button all the background changes, but I dont know how to change the home view controller`s background because they are not in a navigation so I cant use a self.parentViewController casting, so how can I do this? 我有两个视图控制器,一个是Home,另一个是由cocoacontrol动画(MJPopupViewController)显示的,而idn是一个按钮,当您按下一个按钮时,所有背景都会改变,但是我不知道如何更改主视图控制器。的背景,因为它们不在导航中,所以我不能使用self.parentViewController强制转换,那么该怎么做? Heres the code: 这是代码:

- (IBAction)purple:(id)sender {
    self.view.backgroundColor = [UIColor MyPurple];

    HomeViewController *home = (HomeViewController *) self.parentViewController; <-- this didnt work, I also tried only self....

    home.view.backgroundColor = [UIColor purpleColor];


}

So I need your help, Im starting so I hope you understand, thanks! 所以我需要您的帮助,我开始,希望您能理解,谢谢!

Perhaps try: 也许尝试:

HomeViewController HomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(changeColor)
     name:@"changeColor" object:nil];
}

- (void)changeColor
{
    self.view.backgroundColor = [UIColor purpleColor];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

PopUpViewController PopUpViewController

- (IBAction)purple:(id)sender
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeColor" object:nil];
}

I suggest you to change the background color of a 'view' you need to set the backgroundColor property on it. 我建议您更改“视图”的背景色,需要在其上设置backgroundColor属性。 This implies that you have access to it. 这意味着您可以访问它。 If it was all in one controller you would just use 如果全部集中在一个控制器中,您将只使用

self.view.backgroundColor = [UIColor redColor];

If it was in a navigation or similar based app, then you can access a views parentViewController and change the color on it as follows: 如果它在基于导航或类似的应用程序中,则可以访问视图parentViewController并按如下所示更改其颜色:

self.parentViewController.view.backgroundColor = [UIColor redColor];

If this is not possible then you can set an iVar on the second view controller when it is created that contains the instance of the viewController that you want to change the background color on. 如果这不可能,那么可以在创建第二个视图控制器时在其上设置一个iVar,其中包含要更改其背景颜色的viewController实例。

MyViewController* secondViewController = [[MyViewController alloc] init];
secondViewController.bgColorNeedsChangingViewController = self;

Then in the secondViewController's logic 然后在secondViewController的逻辑

self.bgColorNeedsChangingViewController.view.backgroundColor = [UIColor redColor];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个更改viewController的颜色(或主题)? - How do I change the color (or theme) of a viewController from another? 如何从另一个ViewController中的按钮更改单独的ViewController中的textView? - How to change textView in separate ViewController from button in another ViewController? 如何从另一个ViewController更改一个ViewController上的标签文本 - How to change a label text on one ViewController from another ViewController 更改基于当前ViewController的背景颜色StatusBar? - Change the background color StatusBar based on the current ViewController? 更改ViewController的背景颜色,但不更改带有时钟的顶部区域 - Change background color of viewcontroller but not top area with clock ViewController背景颜色不会改变 - ViewController background color won't change Swift:从AppDelegate设置ViewController背景颜色 - Swift: Setting ViewController background color from AppDelegate 如何在 swiftUI 中为 viewController 设置背景颜色? - How to set a background color for the viewController in swiftUI? 如何从另一个ViewController移至现有ViewController - How to move to existing viewcontroller from another viewcontroller 如何从ViewController访问Swift 3中的另一个viewController - How to access from viewController another viewController in Swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM