如何从 CCScrollView 上的按钮更改标签文本?按钮、文本、标签、CCScrollView

2023-09-06 09:37:52 作者:红颜红过她i

我之前发布了一些非常相似的内容,但我认为该帖子令人困惑,所以我正在重做.

I posted something very similar earlier, but I think the post was confusing so I'm redoing it.

这是设置.我正在使用 SpriteBuilder.我有 2 个 CCB 类,MainScene.ccb 和 Scroll.ccb.Scroll.ccb 是一个带有按钮的图层.MainScene 包含一个 CCScrollView 和一个 CCLabelTTF.CCScrollView 正在加载 Scroll.ccb.CCScrollView占据了MainScene界面的一半,另一半是一个CCColor,上面有label.

Here's the setup. I am using SpriteBuilder. I have 2 CCB classes, MainScene.ccb and Scroll.ccb. The Scroll.ccb is a layer with a button on it. MainScene contains a CCScrollView and a CCLabelTTF. The CCScrollView is loading Scroll.ccb. The CCScrollView takes up half of the interface of MainScene, and on the other half is a CCColor that has the label on it.

当我单击 CCScrollView 上的按钮时,我希望 MainScene 上的标签更改其文本.我遇到的问题是标签文本没有改变.但是,如果我在按钮方法中编写一个 NSLog 以在日志中输出文本,那确实可以工作.

When I click the button on the CCScrollView, I want the label on MainScene to change its text. The problem I am having is that the labels text doesn't change. However if I write an NSLog inside the buttons method to output text in the log, that DOES work.

我附上了四张图片,以帮助您更好地了解情况.http://imgur.com/a/77XyJ

I am including four pictures to help you better understand the situation. http://imgur.com/a/77XyJ

在我的主要项目中,我已经坚持了一个多星期.老实说,我已经不知道该做什么了,我尝试了一堆不起作用的东西,而且我的想法已经用完了.

I've been stuck on this for over a week now on my main project. I honestly have no clue what to do anymore, I've tried a bunch of things that didn't work, and I've run out of ideas.

好的,所以我在调试问题方面得到了一点但更进一步.我在滚动视图中插入了一个标签,并将其命名为 Label2.在按钮方法下,我添加了'Label2.string = @Test"',当我运行程序并单击按钮时,滚动视图上的标签发生了变化.所以看起来当按钮被点击时,它在 MainScene 中查找该方法并找到它,但它无法更新不同 CCNode 上的标签.

Okay so I got a little but further in debugging the issue. I inserted a label into the scrollview, and named it Label2. Under the buttons method I added 'Label2.string = @"Test"' and when I ran the program and clicked the button, the label on the scrollview changed. So it seems that when the button is being clicked, it looks in MainScene for the method and finds it, but it can't update a label on a different CCNode.

推荐答案

你有把这个方法链接到点击我"按钮吗?或者尝试创建一个 IBAction ?

Do you have linked this method to the "click me" button ? Or maybe try to create a IBAction ?

-(void)testButton {

       changeLabel.String = @"Changed !";
}

- (IBAction)testButton:(id)sender {

    changeLabel.String = @"Changed !";

}

我的意思是在你的故事板中(使用 ctrl 键创建链接并拖放)

i mean in your storyboard ( create the link with ctrl key and drag drop )

希望这会对你有所帮助.

Hope this will help you.