How do I add a tag with dynamic content to the navigation bar on each view of my apps?

advertisements

I have such a problem: my application in background thread looking for new user messages on the server every second and if there is a new incoming message - app notifies user about it. (this logic for the message look up I have implemented in the app delegate).I have 9 views in the app each with a navigation bar and i want to display a label with new messages counter on this navigation bar (just an integer counter of new unread messages). The problem I don't know how to create a label and connect it with an app delegate. Here is an image of what I want:

In the label there are 13 new messages for the user.And this button must be on all the 9 app's views.

First my idea was to create a UILabel in the AppDelegate file and then create UIBarButtonItem with customView. But how can I add this button on the navigation bar of each view in my application? (I also have other buttons on the navigationBar)

May be this question is little fuzzy but I hope experts will understand it.I really need a help with this.


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;

}