【问题标题】:How to get the int Value of other class's int variable?(XCode5) I'm confused如何获取其他类的 int 变量的 int 值?(XCode5)我很困惑
【发布时间】:2014-08-28 06:16:24
【问题描述】:

这是MyScene.h

#import <SpriteKit/SpriteKit.h>
@interface MyScene : SKScene
@property (nonatomic) int monstersDestroyed;
@end

这是MyScene.m

if( someCondition ){
    self.monsterPassed++;
    NSLog(@"MonsterPassed : %d",self.monsterPassed);
}

控制台将显示“MonsterPassed:0”...“MonsterPassed:1”...等。很好。

这是ViewController.h

#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *DestroyeCountLabel;
@end 

这是ViewController.m

#import "ViewController.h"
#import "MyScene.h"
@interface ViewController ()
@property (nonatomic) AVAudioPlayer * backgroundMusicPlayer;
@end

@implementation ViewController

- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];

MyScene *monsterDestroyerValue = [[MyScene alloc]init];

self.DestroyeCountLabel.text =[NSString stringWithFormat:@"Monster Destroyed:%d",monsterDestroyerValue.monstersDestroyed];
...
}

问题是在ViewController.m 中它不会显示在我的标签中,它只会显示“Monster Destroyed: 0”。为什么?我已经搜索过,但找不到任何解决方案。

【问题讨论】:

  • 您正在创建场景的新实例并尝试从中检索值。

标签: ios xcode5 public object-code


【解决方案1】:

您只需设置 DestroyeCountLabel 的文本一次。在此之后设置 monstersDestroyed 变量不会自动更新 DestroyeCountLabel。有无数种方法可以做到这一点。我的建议是在深入研究之前阅读一些 Apple 的教程,以便更好地掌握编程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 2018-08-31
    • 2015-03-01
    • 1970-01-01
    • 2011-04-11
    • 2023-01-18
    相关资源
    最近更新 更多