为什么我的UILabel不会显示NSInteger [英] why wont my UILabel display a NSInteger

查看:104
本文介绍了为什么我的UILabel不会显示NSInteger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewController和GameController类,它只是NSObject的子类。视图控制器有一个链接到它的按钮,并触发一个进入GameController类的IBAction。在GameController类的init中是一个CADisplayLink,它将一个添加到一个名为score的int中。在调试器中,分数会上升,但标签只会显示0。

I have a ViewController, and GameController class which is just subclassed from NSObject. The view controller has one button linked to it and fires a IBAction that inits a GameController class. In the init of the GameController class is a CADisplayLink that adds one to an int named score. In the debugger the score is going up, but the label will only display 0.

在ViewController中

in ViewController

-(void)setScore:(NSInteger)gameScore{
NSString *string = [NSString stringWithFormat:@"%i", gameScore];
scoreLabel.text = string;
NSLog(@"the score is %i", gameScore);
}

-(IBAction)play:(id)sender{
gameController = [[GameController alloc] init];
//in my header is GameController* gameController;
}

在GameController中

in GameController

-(id)init{
self = [super init];
if (self) {

viewController = [[ViewController alloc] init];
//in header ViewController* viewController;
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLoop:)];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode: NSRunLoopCommonModes];

//in the header CADisplayLink* displayLink;
}
return self;

}

-(void)gameLoop:(CADisplayLink *)sender{
deltaScore++;
if (deltaScore >= 20) {
deltaScore -= 20;
score++;
//deltaScore and score are NSIntegers
[viewController setScore:score];
}
}


推荐答案

看仔细阅读你的代码。您的ViewController的'-play:'方法创建一个新的GameController。你的GameController的'-init'创建了一个新的ViewController。这真的是你想要的吗?

Look carefully at your code. Your ViewController's ‘-play:‘ method creates a new GameController. Your GameController's ‘-init‘ creates a new ViewController. Is that really what you want?

你没有给我们太多的东西,但你的游戏控制器或你的视图控制器似乎应该在收费并创建另一个实例。这里有大约一百万个关于如何让一个对象将一些数据发送到另一个对象的问题,但它们都归结为:将信息从对象a传递到对象b,需要指向b的指针。然后,A可以使用该指针向b发送消息。

You haven't given us much to go on, but it seems likely that either your game controller or your view controller should be "in charge" and create one instance of the other. There are about a million questions here on SO related to how to get one object to send some data to the another, but they all boil down to this: to pass information from object a to object b, a needs a pointer to b. A can then use that pointer to send messages to b.

因此,例如,您的视图控制器会创建游戏控制器。这意味着视图控制器有一个指向游戏控制器的指针,它可以使用实例变量或属性来保存它。如果游戏控制器需要将消息发送回视图控制器,则需要一个指向视图控制器的指针。视图控制器可以在创建游戏控制器时或之后的某个时刻提供该指针(自身)。为此,游戏控制器需要一种可以接受指向视图控制器的指针的方法。也许您决定在创建时执行此操作,因此您将游戏控制器的initialize toon方法更改为'-initWithDelegate:(id)',定义GameControllerDelegate协议,然后在ViewController类中采用该协议。你应该这样做,而不仅仅是使用'-initWithViewController :( ViewController *)',因为它的好风格可以防止GameController依赖于帮助它的对象的特定类 - GameController可能只关心它的助手实现了一些方法,它不需要知道它的助手是一个ViewController。

So, for example, your view controller creates the game controller. That means the view controller has a pointer to the game controller, and it can use an instance variable or property to save that. If the game controller needs to send messages back to the view controller, it'll need a pointer to the view controller. The view controller can supply that pointer (to itself) when the game controller is created, or at some point afterward. To facilitate that, the game controller needs a method that can accept the pointer to the view controller. Maybe you decide to do it at creation, so you change the game controller's initialize toon method to ‘-initWithDelegate:(id)‘, define a GameControllerDelegate protocol, and then adopt that protocol in your ViewController class. You'd do this rather than just using ‘-initWithViewController:(ViewController*)‘ because its good style to prevent GameController from depending on the particular class of the object that's helping it -- GameController probably only cares that its helper implements a few methods, it doesn't need to know that it's helper is a ViewController.

这篇关于为什么我的UILabel不会显示NSInteger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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