【发布时间】:2014-12-15 15:35:25
【问题描述】:
目标和问题:
每经过一秒,“regularBubbleCount”的值(RWGameData 的一个变量)就会增加 1。我试图通过将“regularBubbleCount”的新值传递给“regularBubLabel”UILabel 中的主视图控制器。我正在尝试使用以下代码行来做到这一点,
_regularBubLabel.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].regularBubbleCount];
显然这不起作用,因为 'regularBubLabel' 不是 'timerCalled' 方法所在的 RWGameData 类的对象。如何从 RWGameData 类中更改“regularBubLabel”的值?
RWGameData.h
#import <Foundation/Foundation.h>
@interface RWGameData : NSObject <NSCoding>
@property (assign, nonatomic) long regularBubbleCount;
@property (assign, nonatomic) BOOL dataIsInitialized;
+(instancetype)sharedGameData;
-(void)reset;
-(void)save;
-(void)timerSetup;
-(void)timerCalled;
@end
RWGameData.m
#import "RWGameData.h"
@implementation RWGameData
static NSString* const SSGameDataRegularBubbleCountKey = @"regularBubbleCount";
static NSString* const SSGameDataIsInitializedKey = @"dataIsInitializedKey";
+ (instancetype)sharedGameData {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [self loadInstance];
});
return sharedInstance;
}
-(void)reset {
self.regularBubbleCount = 0;
self.dataIsInitialized = true;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeDouble:self.regularBubbleCount forKey: SSGameDataRegularBubbleCountKey];
[encoder encodeBool:self.dataIsInitialized forKey: SSGameDataIsInitializedKey];
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
self = [self init];
if (self) {
_regularBubbleCount = [decoder decodeDoubleForKey: SSGameDataRegularBubbleCountKey];
_dataIsInitialized = [decoder decodeBoolForKey: SSGameDataIsInitializedKey];
}
return self;
}
+(NSString*)filePath
{
static NSString* filePath = nil;
if (!filePath) {
filePath =
[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
stringByAppendingPathComponent:@"gamedata"];
}
return filePath;
}
+(instancetype)loadInstance
{
NSData* decodedData = [NSData dataWithContentsOfFile: [RWGameData filePath]];
if (decodedData) {
RWGameData* gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodedData];
return gameData;
}
return [[RWGameData alloc] init];
}
-(void)save
{
NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: self];
[encodedData writeToFile:[RWGameData filePath] atomically:YES];
}
- (void)timerSetup { // to be called from delegate didFinishLaunching….
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled) userInfo:nil repeats:YES];
}
-(void)timerCalled
{
[RWGameData sharedGameData].regularBubbleCount++;
/* THE ISSUE IS HERE */
_regularBubLabel.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].regularBubbleCount];
[[RWGameData sharedGameData] save];
} NSLog(@"Regular Bubble Count: %li", [RWGameData sharedGameData].regularBubbleCount);
}
@end
PrimaryViewController.h
#import <UIKit/UIKit.h>
#import "RWGameData.h"
@interface PrimaryViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *regularBubLabel;
@end
PrimaryViewController.m
#import "PrimaryViewController.h"
@interface PrimaryViewController ()
@end
@implementation PrimaryViewController
{
NSString *bubbleImage;
UIImage *backgroundImage;
UIImageView *backgroundImageView;
int r;
int i;
}
- (void)viewDidLoad {
[super viewDidLoad];
backgroundImage = [UIImage imageNamed:@"background_new.png"];
backgroundImageView=[[UIImageView alloc]initWithFrame:self.view.frame];
backgroundImageView.image=backgroundImage;
[self.view insertSubview:backgroundImageView atIndex:0];
}
- (void)viewDidAppear:(BOOL)animated {
_regularBubLabel.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].regularBubbleCount];
_premiumBubLabel.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].premiumBubbleCount];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)increment {
if ([RWGameData sharedGameData].megaBubblePopValue == 0) {
[RWGameData sharedGameData].megaBubblePopValue++;
[[RWGameData sharedGameData] save];
}
if ([@"mysterybubble.png" isEqual:bubbleImage]) {
[RWGameData sharedGameData].premiumBubbleCount += 2;
_premiumBubLabel.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].premiumBubbleCount];
} else if ([@"megaBubbleLarge30.png" isEqual:bubbleImage]) {
[RWGameData sharedGameData].regularBubbleCount += [RWGameData sharedGameData].megaBubblePopValue;
_regularBubLabel.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].regularBubbleCount];
} i++;
}
- (IBAction)save {
[[RWGameData sharedGameData] save];
}
- (IBAction)setBubbleStatus {
r = arc4random_uniform(400);
if (r <= 1) {
bubbleImage = @"mysterybubble.png";
[_megaBubbleButton setImage:[UIImage imageNamed:bubbleImage] forState:UIControlStateNormal];
NSLog(@"Roll SUCCESS. [%i] %i", i, r);
} else {
bubbleImage = @"megaBubbleLarge30.png";
[_megaBubbleButton setImage:[UIImage imageNamed:bubbleImage] forState:UIControlStateNormal];
NSLog(@"Roll FAIL. [%i] %i", i, r);
}
}
@end
【问题讨论】:
-
我很感激投反对票 - 我会在未来努力让我的问题变得更强大 - 但如果你有任何信息可供我使用,我也会感谢你在投反对票的同时发表评论.
-
我强烈建议您将问题集中在所需的代码上。
-
请删除除相关代码之外的所有代码,并指出导致问题的确切代码行。
-
我有几个问题。一、RWGameData和PrimaryViewController在哪里实例化? RWGameData::regBubleLabel 的更新是否发生在主线程上?
-
@ReinhardMänner 我已经适当地更新了主题。谢谢!
标签: ios objective-c iphone xcode