Jon
Jon

Reputation: 4732

Communicate between App Delegate and VC

I have a method in my app delegate that updates the badge count for the app's icon. I want this method to also update something in another VC which is visible throughout the app (like a notification center) I have.

How can I do this?

The method looks like:

- (void) remedyERxInterface:(RSeRxInterface *)eRxInterface 
             didSynchronize:(BOOL)success pendingMessageCounts:(NSDictionary *)pendingMessageCounts {

    NSInteger messageTotal = 0;
    NSInteger refillRequests = [[pendingMessageCounts objectForKey:kRSQueuesRefillCount] intValue];
    NSInteger problems = [[pendingMessageCounts objectForKey:kRSQueuesProblemCount] intValue];
    NSInteger messages = [[pendingMessageCounts objectForKey:kRSQueuesMessageCount] intValue];

    if (0) {
        if (self.eRxModuleInboxMessageCount) {
            messageTotal = refillRequests + problems + messages;
        } else {
            messageTotal = refillRequests + problems + messages + 12;
        }
    } else {
        //Production
        messageTotal = refillRequests + problems + messages;
    }

    [UIApplication sharedApplication].applicationIconBadgeNumber = messageTotal;
    self.eRxModuleInboxMessageCount = messageTotal;
}

Upvotes: 0

Views: 173

Answers (2)

jbat100
jbat100

Reputation: 16827

You mention a notification center. Have you looked into using NSNotifications through the NSNotificationCenter? It is ideal for a central entity to broadcast information without knowing who listens. You can just register your view controllers for these notifications with callbacks which will be called synchronously.

Upvotes: 2

Jim
Jim

Reputation: 73936

If some information needs to be visible throughout the app, create a manager class that is responsible for storing the information. Create a convenience method to access a shared instance of this class. When an event occurs that means you have to update the information, tell the shared instance about the event. For all parts of your user interface that need to display this information, use Key-Value Observing (KVO) against the shared instance to be notified when any of its properties change.

Upvotes: 0

Related Questions