为什么我的整数不在视图控制器之间传输? [英] Why isn't my integer transferring between view controllers?

查看:94
本文介绍了为什么我的整数不在视图控制器之间传输?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在游戏中为额外的生命设置IAP,但在我这样做之前,我想测试它是否会从我的购买View Controller转移到我的游戏View Controller而不是。在我的购买VC中我有



I am going to be setting up IAP for extra lives in my game, but before I do that I wanted to test to see if it would transfer from my purchasing View Controller to my game View Controller and it's not. In my purchasing VC I have

-(void)StartView{
//Life = [[NSUserDefaults standardUserDefaults] integerForKey:@"Life"]; (If in the code then it will not update the LifeCounter.text)
livesUpdated = [NSTimer scheduledTimerWithTimeInterval:0.0001 target:self selector:@selector(LiveUpdatedCheck) userInfo:nil repeats:YES];
}


-(void)LiveUpdatedCheck{
LifeCounter.text = [NSString stringWithFormat:@"%li", (long)Life];
}


-(void)LiveUpdatedCheck{
LifeCounter.text = [NSString stringWithFormat:@"%li", (long)Life];
}


-(IBAction)TenLifePurchase:(id)sender{
    Life = Life + 10;
}





然后在我的游戏VC中我有





Then in my game VC I have

-(IBAction)StartGame:(id)sender{
Life = [[NSUserDefaults standardUserDefaults] integerForKey:@"Life"];
LifeCounter.text = [NSString stringWithFormat:@"%li", (long)Life];
}





'Life'在我的两个.h文件中被声明为NSInteger,我使用了userDefaults就像这样在两个视图控制器之间移动我的高分,并通过id整数改变球的颜色。我该如何解决这个问题?可能是因为在我的购买控制器和我的游戏控制器之间,你必须通过一个没有生命信息的主屏幕?此外,当您在查看游戏控制器后返回购买的控制器时,它会从任何数字重置为0,但如果您查看任何其他控制器然后返回购买,则该数字将存储而不会重置为0.



'Life' is declared as an NSInteger in both of my .h files and I used userDefaults just like this to move my high score between 2 view controllers and the same for changing the color of a ball via id integers. How can I fix this problem? Could it be because in-between my purchase controller and my game controller you have to go through a main screen that has no information on 'Life'? Also when you go back to the purchased controller after viewing the game controller it resets back to 0 from whatever number it was, but if you view any other controller then back to purchased then the number will store and not reset back to 0.

推荐答案

这篇关于为什么我的整数不在视图控制器之间传输?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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