【问题标题】:Changing label text on SKLabelNode crashes app更改 SKLabelNode 上的标签文本会使应用程序崩溃
【发布时间】:2015-03-25 21:29:54
【问题描述】:

我试图在运行时更改 SKLabelNode 中的文本,但它使应用程序崩溃并出现错误:CRASH: -[SKLabelNode label]: unrecognized selector sent to instance

我从selectNodeForTouch:(CGPoint)touchlocation 方法调用它,如下所示:

        if ([[node name]isEqualToString:@"e"]) {
                        // add current riddle to favourites and change the icon of the star
                        button *btn = (button *)[self nodeAtPoint:touchLocation];
                        btn.label.text = @"d";
                        [(GameViewController *)self.view.window.rootViewController addToFavourites:_currentTomhais];
                        NSLog(@"Favourited");
        }

按钮对象有如下接口:

@interface button : SKSpriteNode
@property (nonatomic, retain) SKLabelNode *label;

并且在button.m文件中如下初始化

    @implementation button
-(instancetype) initWithString:(NSString *)character fontNamed:(NSString *)font size:(float)size{
    self = [super init];
    if (self) {
        [self setSize:CGSizeMake(size, size)];
        //icon Text
        _label = [[SKLabelNode alloc] initWithFontNamed:font];
        _label.name = character;
        _label.fontSize = size;
        _label.fontColor = [UIColor colorWithRed:150.0f/255.0f
                                          green:166.0f/255.0f
                                           blue:173.0f/255.0f
                                          alpha:1];

        [_label setText:character];
        _label.position = CGPointMake(0, 0);
        [self addChild:_label];

    }
    return self;
}

知道如何在运行时更改此项目上的文本而不会使应用程序崩溃吗?

【问题讨论】:

  • 什么时候崩溃?何时发生触摸?
  • 您将选择器“标签”发送到无法识别的 SKLabelNode。难道被触摸的节点实际上是一个 SkLabelNode,所以当您尝试在 btn.label.text = @"d"; 处设置文本时,您正在向 SkLabelNode 发送“标签”?
  • 很好看的@joaquinzrr - 就是这样。谢谢。

标签: ios sprite-kit sklabelnode


【解决方案1】:

您将选择器“标签”发送到无法识别的 SKLabelNode。触摸的节点实际上是一个 SkLabelNode,因此当您尝试将文本设置为 btn.label.text = @"d";

时,您正在向 SkLabelNode 发送“标签”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    相关资源
    最近更新 更多