why wont my UILabel display a NSInteger

前端 未结 6 1653
予麋鹿
予麋鹿 2021-01-26 07:08

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 GameC

相关标签:
6条回答
  • 2021-01-26 07:33

    score is not updated because you are creating new instance of view controller in game controller, you somehow need to pass the object of view controller to game controller and then use the same object to call method setScore.

    0 讨论(0)
  • 2021-01-26 07:35

    In GameCenter the Game Score is of type long long int so use long long in your methods and return type if using anywhere. When you want to so it on the label use

    yourLabel.text = [NSString stringWithFormat:@"%lld",yourscore];
    
    0 讨论(0)
  • 2021-01-26 07:43

    %i is an invalid format specifier; use %d for signed 32-bit integer or %u for an unsigned 32-bit integer

    0 讨论(0)
  • 2021-01-26 07:49

    Usually, when the debugger gets information correctly but the UI isn't updating is because the operation to update the UI isn't performed on the main thread. Try this:

    dispatch_async(dispatch_get_main_queue(), ^{ [viewController setScore:score]; });
    
    0 讨论(0)
  • 2021-01-26 07:50

    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?

    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.

    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.

    0 讨论(0)
  • 2021-01-26 07:53

    Have you tried?

    NSLog(@"the score is %lu", gameScore);
    

    It should work for you.

    0 讨论(0)
提交回复
热议问题