在我的TabViewController中创建对所选选项卡的引用?

时间:2015-02-06 20:12:18

标签: ios

RWGameData.h

#import <Foundation/Foundation.h>

@class RWGameData;
@protocol RWGameStateProtocol <NSObject>
-(void)StateUpdateForGameData:(RWGameData*)data;
@end

@interface RWGameData : NSObject <NSCoding>

@property (weak) id<RWGameStateProtocol> delegate;

@property (assign, nonatomic) long regularBubbleCount;
@property (assign, nonatomic) long premiumBubbleCount;

@property (assign, nonatomic) long megaBubbleUpgradeTier;
@property (assign, nonatomic) long bubbleFactoryUpgradeTier;
@property (assign, nonatomic) long bubblersUpgradeTier;
@property (assign, nonatomic) long mysteryBubbleUpgradeTier;
@property (assign, nonatomic) long bubbleBankUpgradeTier;

@property (assign, nonatomic) int megaBubblePopValue;
@property (assign, nonatomic) int bubbleFactoryTickValue;

@property (assign, nonatomic) long bubbleBankCapacity;

@property (assign, nonatomic) int collectionBallsQuantity;
@property (assign, nonatomic) int collectionGlowsticksQuantity;
@property (assign, nonatomic) int collectionFlowersQuantity;
@property (assign, nonatomic) int collectionStuffedAnimalsQuantity;
@property (assign, nonatomic) int collectionEasterEggsQuantity;

@property (assign, nonatomic) int currentXP;
@property (assign, nonatomic) int targetXP;
@property (assign, nonatomic) int level;

@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 SSGameDataPremiumBubbleCountKey = @"premiumBubbleCount";

static NSString* const SSGameDataMegaBubbleUpgradeTierKey = @"megaBubbleUpgradeTier";
static NSString* const SSGameDataBubbleFactoryUpgradeTierKey = @"bubbleFactoryUpgradeTier";
static NSString* const SSGameDataBubblersUpgradeTierKey = @"bubblersUpgradeTier";
static NSString* const SSGameDataMysteryBubbleUpgradeTierKey = @"mysteryBubbleUpgradeTier";
static NSString* const SSGameDataBubbleBankUpgradeTierKey = @"bubbleBankUpgradeTier";

static NSString* const SSGameDataMegaBubblePopValueKey = @"megaBubblePopValueKey";
static NSString* const SSGameDataBubbleFactoryTickValueKey = @"bubbleFactoryTickValueKey";

static NSString* const SSGameDataBubbleBankCapacityKey = @"bubbleBankCapacityKey";

static NSString* const SSGameDataCollectionBallsQuantityKey = @"collectionBallsQuantityKey";
static NSString* const SSGameDataCollectionGlowsticksQuantityKey = @"collectionGlowsticksQuantityKey";
static NSString* const SSGameDataCollectionFlowersQuantityKey = @"collectionFlowersQuantityKey";
static NSString* const SSGameDataCollectionStuffedAnimalsQuantityKey = @"collectionStuffedAnimalsQuantityKey";
static NSString* const SSGameDataCollectionEasterEggsQuantityKey = @"collectionEasterEggsQuantityKey";

/*static NSString* const SSGameDataCollectionBallsModifierKey = @"collectionBallsModifierKey";
static NSString* const SSGameDataCollectionGlowsticksModifierKey = @"collectionGlowsticksModifierKey";
static NSString* const SSGameCollectionFlowersModifierKey = @"collectionFlowersModifierKey";
static NSString* const SSGameCollectionStuffedAnimalsModifierKey = @"collectionStuffedAnimalsModifierKey";
static NSString* const SSGameCollectionEasterEggsModifierKey = @"collectionEasterEggsModifierKey";*/

static NSString* const SSGameDataCurrentXPKey = @"currentXPKey";
static NSString* const SSGameDataTargetXPKey = @"targetXPKey";
static NSString* const SSGameDataLevelKey = @"levelKey";

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.premiumBubbleCount = 0;

    self.megaBubbleUpgradeTier = 0;
    self.bubbleFactoryUpgradeTier = 0;
    self.bubblersUpgradeTier = 0;
    self.mysteryBubbleUpgradeTier = 0;
    self.bubbleBankUpgradeTier = 0;

    self.megaBubblePopValue = 1;
    self.bubbleFactoryTickValue = 1;

    self.bubbleBankCapacity = 500;

    self.collectionBallsQuantity = 0;
    self.collectionGlowsticksQuantity = 0;
    self.collectionFlowersQuantity = 0;
    self.collectionStuffedAnimalsQuantity = 0;
    self.collectionEasterEggsQuantity = 0;

    self.currentXP = 0;
    self.targetXP = 83;
    self.level = 1;

    self.dataIsInitialized = true;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeDouble:self.regularBubbleCount forKey: SSGameDataRegularBubbleCountKey];
    [encoder encodeDouble:self.premiumBubbleCount forKey: SSGameDataPremiumBubbleCountKey];

    [encoder encodeDouble:self.megaBubbleUpgradeTier forKey: SSGameDataMegaBubbleUpgradeTierKey];
    [encoder encodeDouble:self.bubbleFactoryUpgradeTier forKey: SSGameDataBubbleFactoryUpgradeTierKey];
    [encoder encodeDouble:self.bubblersUpgradeTier forKey: SSGameDataBubblersUpgradeTierKey];
    [encoder encodeDouble:self.mysteryBubbleUpgradeTier forKey: SSGameDataMysteryBubbleUpgradeTierKey];
    [encoder encodeDouble:self.bubbleBankUpgradeTier forKey: SSGameDataBubbleBankUpgradeTierKey];

    [encoder encodeDouble:self.megaBubblePopValue forKey: SSGameDataMegaBubblePopValueKey];
    [encoder encodeDouble:self.bubbleFactoryTickValue forKey: SSGameDataBubbleFactoryTickValueKey];

    [encoder encodeDouble:self.bubbleBankCapacity forKey: SSGameDataBubbleBankCapacityKey];

    [encoder encodeDouble:self.collectionBallsQuantity forKey: SSGameDataCollectionBallsQuantityKey];
    [encoder encodeDouble:self.collectionGlowsticksQuantity forKey: SSGameDataCollectionGlowsticksQuantityKey];
    [encoder encodeDouble:self.collectionFlowersQuantity forKey: SSGameDataCollectionFlowersQuantityKey];
    [encoder encodeDouble:self.collectionStuffedAnimalsQuantity forKey: SSGameDataCollectionStuffedAnimalsQuantityKey];
    [encoder encodeDouble:self.collectionEasterEggsQuantity forKey: SSGameDataCollectionEasterEggsQuantityKey];

    [encoder encodeDouble:self.currentXP forKey: SSGameDataCurrentXPKey];
    [encoder encodeDouble:self.targetXP forKey: SSGameDataTargetXPKey];
    [encoder encodeDouble:self.level forKey:SSGameDataLevelKey];

    [encoder encodeBool:self.dataIsInitialized forKey: SSGameDataIsInitializedKey];
}

- (instancetype)initWithCoder:(NSCoder *)decoder
{
    self = [self init];
    if (self) {
        _regularBubbleCount = [decoder decodeDoubleForKey: SSGameDataRegularBubbleCountKey];
        _premiumBubbleCount = [decoder decodeDoubleForKey: SSGameDataPremiumBubbleCountKey];

        _megaBubbleUpgradeTier = [decoder decodeDoubleForKey: SSGameDataMegaBubbleUpgradeTierKey];
        _bubbleFactoryUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubbleFactoryUpgradeTierKey];
        _bubblersUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubblersUpgradeTierKey];
        _mysteryBubbleUpgradeTier = [decoder decodeDoubleForKey: SSGameDataMysteryBubbleUpgradeTierKey];
        _bubbleBankUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubbleBankUpgradeTierKey];

        _megaBubblePopValue = [decoder decodeDoubleForKey: SSGameDataMegaBubblePopValueKey];
        _bubbleFactoryTickValue = [decoder decodeDoubleForKey: SSGameDataBubbleFactoryTickValueKey];

        _bubbleBankCapacity = [decoder decodeDoubleForKey: SSGameDataBubbleBankCapacityKey];

        _collectionBallsQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionBallsQuantityKey];
        _collectionGlowsticksQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionGlowsticksQuantityKey];
        _collectionFlowersQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionFlowersQuantityKey];
        _collectionStuffedAnimalsQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionStuffedAnimalsQuantityKey];
        _collectionEasterEggsQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionEasterEggsQuantityKey];

        _currentXP = [decoder decodeDoubleForKey: SSGameDataCurrentXPKey];
        _targetXP = [decoder decodeDoubleForKey: SSGameDataTargetXPKey];
        _level = [decoder decodeDoubleForKey: SSGameDataLevelKey];

        _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;
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled) userInfo:nil repeats:YES];
}

-(void)timerCalled
{
    //NSLog(@"Timer Called");]
    if ([RWGameData sharedGameData].bubbleFactoryUpgradeTier > 0) {
        if ([RWGameData sharedGameData].regularBubbleCount < [RWGameData sharedGameData].bubbleBankCapacity) {
            [RWGameData sharedGameData].regularBubbleCount += [RWGameData sharedGameData].bubbleFactoryTickValue;
            [[RWGameData sharedGameData] save];


        } else {
            NSLog(@"Capacity Reached! Capacity: %li", [RWGameData sharedGameData].bubbleBankCapacity);
        }
        [self.delegate StateUpdateForGameData:self];

    } NSLog(@"Regular Bubble Count: %li", [RWGameData sharedGameData].regularBubbleCount);
}

@end

PrimaryViewController.h

#import <UIKit/UIKit.h>
#import "RWGameData.h"

@interface PrimaryViewController : UIViewController <RWGameStateProtocol>

@property (strong, nonatomic) IBOutlet UILabel *regularBubLabel;

@end

我希望能够在timerCalled方法中更改regularBubLabel的值。谢谢你的时间。

包含我所要求的整个RWGameData类。感谢。

AppDelegate.h

#import <UIKit/UIKit.h>
#import "RWGameData.h"
#import "PrimaryViewController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) RWGameData *gameData;


@end

然后参见#RWGameData.m

的+(instancetype)loadInstance

2 个答案:

答案 0 :(得分:0)

如果您可以提供有关您要实现的目标的更多详细信息。

如果我理解正确,您想知道哪个标签处于活动状态 所有你要做的就是获取 tab.selectedIndex 属性,它将返回给你的int表示activTabView的索引然后你可以做任何事情,比如更改所选索引的文本

希望这有帮助。

答案 1 :(得分:0)

从你的代码我可以看到你的PrimaryViewController正在确认RWGameData的协议,即

@interface PrimaryViewController : UIViewController <RWGameStateProtocol>

所以,将RWGameData的委托设置为PrimaryViewController对象,然后在PrimaryViewController.m中实现此委托方法,如

-(void)StateUpdateForGameData:(RWGameData*)data
{
  //now update label
  self.regularBubLabel.text = @"asdasd";
}

编辑:

在PrimaryViewController.m viewDidLoad方法中添加此行

[RWGameData  sharedGameData].delegate = self;

timercalled方法应该像

-(void)timerCalled
{
    //NSLog(@"Timer Called");]
    if ([RWGameData sharedGameData].bubbleFactoryUpgradeTier > 0) {
        if ([RWGameData sharedGameData].regularBubbleCount < [RWGameData sharedGameData].bubbleBankCapacity) {
            [RWGameData sharedGameData].regularBubbleCount += [RWGameData sharedGameData].bubbleFactoryTickValue;
            [[RWGameData sharedGameData] save];


        } else {
            NSLog(@"Capacity Reached! Capacity: %li", [RWGameData sharedGameData].bubbleBankCapacity);
        }

  if(self.delegate && [self.delegate respondsToSelector:@selector(StateUpdateForGameData:)])
        [self.delegate StateUpdateForGameData:self];

    } NSLog(@"Regular Bubble Count: %li", [RWGameData sharedGameData].regularBubbleCount);
}