更改不同图层中标签的标签颜色 [英] Changing label color of a label in a different layer

查看:330
本文介绍了更改不同图层中标签的标签颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个场景 MainScene 其中 CCScrollView UpgradesScroller 包含称为 UpgradesNew 的层。我有一个按钮,当点击 MainScene ,应该设置 hasItem 布尔到 code>,然后当 UpgradesNew 未被隐藏时,应根据布尔值的输出更改标签颜色。

So I have a scene called MainScene where CCScrollView UpgradesScroller containing layer called UpgradesNew is located. I have a button that when tapped in MainScene, should set hasItem boolean to YES and then when the layer UpgradesNew is unhidden, a labels color should be changed based on the output of the boolean.

这是我试过的。

设置 hasItem 为true:

-(void) buyItem {
        hasItem = true;
    }
}

MainScene 这是 UpgradesNew 未被隐藏时使用的方法。

In MainScene this is the method used when UpgradesNew is unhidden.

-(void)Upgrades {
    UpgradesNew *upNew = [[UpgradesNew alloc]init];
    [upNew changeColor];
    if (upgradesScroller.visible == NO) {
        upgradesScroller.visible = YES;
    } else if (upgradesScroller.visible == YES) {
        upgradesScroller.visible = NO;
    }
}

UpgradesNew

-(void)changeColor {
    if (hasItem == true) {
        label.color = [CCColor greenColor];
        NSLog(@"changecolor");
    }
}

我认为问题是因为我分配空实例UpgradesNew。但我不知道如何以正确的方式做。

I think the problem is because I'm allocating an empty instance of UpgradesNew. But I'm not sure how to do it the right way.

推荐答案

问题有点混乱,信息是位不完整,但我只能假设问题在这里:

The question is a little confusing and the information is a bit incomplete, but I can only assume the problem is here:

UpgradesNew *upNew = [[UpgradesNew alloc]init];

我猜猜 UpgradesNew 您已经实例化并在屏幕上显示的视图或视图控制器。当你调用 [[UpgradesNew alloc] init]; 时,你正在创建一个新对象。你需要的是引用当前存在的 UpgradesNew 对象,你打算更改。您需要在此现有对象上调用 changeColor 方法,而不是创建一个新对象。

I'm guessing UpgradesNew is some sort of view or view controller you've already instantiated and have on your screen. When you call [[UpgradesNew alloc] init];, you're creating a new object. What you need instead is a references to the currently existing UpgradesNew object that you intend to change. You need to call the changeColor method on this existing object rather than creating a new one.

编辑:想要如何获得对正确对象的引用的帮助,你需要更多的细节在你的问题 - 写,这些对象之间的关系没有太多的提示。

If you want help on how to get a reference to the right object, you need a lot more detail in your question--as written, there's not much hint at the relationship between these objects.

这篇关于更改不同图层中标签的标签颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆