Changing label color of a label in a different layer

穿精又带淫゛_ 提交于 2019-12-13 08:27:15

问题


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.

Here is what I tried.

Setting hasItem to true:

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

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;
    }
}

In UpgradesNew

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

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.


回答1:


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];

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.

EDIT: 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.



来源:https://stackoverflow.com/questions/24025776/changing-label-color-of-a-label-in-a-different-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!