If I understood your question right, One option is Singleton Design Pattern
look at here for details.
So with singleton you will set a global instance then call it when you need it.
Right click your code and add a Objective-c class name it SingletonClass, make it sub class of NSObject
below example is integer
change it to a string
or any type if you want,
in your SingletonClass.h
n your SingletonClass.h
#import <Foundation/Foundation.h>
@interface SingletonClass : NSObject
@property int thisIsCounter;
+ (SingletonClass *)sharedInstance;
@end
in your SingletonClass.m
#import "SingletonClass.h"
@implementation SingletonClass
@synthesize thisIsCounter=_thisIsCounter;
+ (SingletonClass *)sharedInstance
{
static SingletonClass *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[SingletonClass alloc] init];
// Do any other initialisation stuff here
});
return sharedInstance;
}
- (id)init {
if (self = [super init]) {
// set a singleton managed object , variable in your case
_thisIsCounter=self.thisIsCounter;
}
return self;
}
@end
and import your singletonclass in your desired class in your case its all classes
#import "SingletonClass.h"
//in your app delegate when you fetch data increase singleton variable or decrease it if you want , basically you have a global variable that you can use in your all classes
-(IBAction)plus
{
SingletonClass *sharedInstance = [SingletonClass sharedInstance];
sharedInstance.thisIsCounter =sharedInstance.thisIsCounter + 1;
}
Code is not tested , improved it as needed.
//Look at me now!!!!!
Above will set your global instance, now to call this in your viewcontroller every second (this is tricky because you may need to use main thread and it will get disturbed with UI events) you need :
How update a label periodically on iOS (every second)?
UILabel text not being updated
- (void)viewDidLoad
{
[super viewDidLoad];
NSTimer* timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void) updateLabel
{
SingletonClass *sharedInstance = [SingletonClass sharedInstance];
self.button.text= sharedInstance.thisIsCounter;
}