iOS - Changing tabbar badge from other class

uitabbaritem badge
uitabbarbutton
uitabbarcontroller
uitabbaritem tag
uitabbar appearance ios 13
uitabbaritem custom view
tab bar item badge value
ios tab bar icon generator

I've searched a lot but could not find an answer and therefore decided to ask you :).

I have an application with some views. After logging in I create a UITabBarController with 3 tabs.

Now I wish to change the second tab's badge based on how many notifications the user has.

This core works when called in the viewdidload method:

NSString *badgeValue = [NSString stringWithFormat:@"%i", [cacheNotifications count]];
if([cacheNotifications count] != 0){
    [[[[[self tabBarController] viewControllers] objectAtIndex: 1] tabBarItem] setBadgeValue:badgeValue];
}

However, I have a daemon running in the background that checks for notifications every 30 seconds. It would be great if I could change the badge from this daemon.

When I call something like this:

PlatformViewController *theInstance = [[PlatformViewController alloc] init];
    [theInstance updateNotificationsBadge];

It does call the function but does not update the badge. With or without the performSelectorOnMainThread.

updateNotificationsBadge:

-(void) updateNotificationsBadge{
NSString *badgeValue = [NSString stringWithFormat:@"%i", [cacheNotifications count]];
NSLog(@"Length here is: %i", [cacheNotifications count]);
if([cacheNotifications count] > 0){
    NSLog(@"call..");
    [self performSelectorOnMainThread:@selector(setNotification:)
                                    withObject:badgeValue
                                 waitUntilDone:YES];


}

}

-(void)setNotification:(NSString*)badge{
NSLog(@"Called. %@", badge);
[[[[[self tabBarController] viewControllers] objectAtIndex: 1] tabBarItem] setBadgeValue:badge];

}

How could I fix this?

Thanks in advance!

EDIT:

cacheNotifications is a global variable declared in globalVars.m. It does not get reinitialized when I call a new instance.

I call the code below from daemonClass.m

PlatformViewController *theInstance = [[PlatformViewController alloc] init];
[theInstance updateNotificationsBadge];

Instead of creating a new instance for platformViewController, you need to use existing reference. When you create a new one, cacheNotification array would not be initialized and no contents in it. So it will always returns 0.

and UITabBarController is a containerViewController contains all the viewControllers. So you don't need to change the tab badgeValue from the other class. You can simply change it from any class.

and in your setNotification: method, change the badgeValue like this.

[[[[self tabBarController] tabBar] items] objectAtIndex:1] setBadgeValue:badge];

Using UITabBarAppearance for Tab Bar Changes in iOS 13, With iOS 13, Apple finally made some long overdue changes to support more on iPad within UISplitViewController classes and some other scenarios in iOS 13 . New and Improved! With the new classes in iOS 13 you now set the tab bar background color with Setting Tab Bar Item Badge Appearance. When focus leaves the tab bar, the tab bar remains fixed at the top of the screen by default. To create an interface where the tab bar doesn't remain fixed, but instead scrolls with the content, set the tab Bar Observed Scroll View property to the appropriate scroll view. In iOS, the tab bar always stays pinned at the bottom of the screen.

You should use the same instance of class rather creating the new one. Which destroy the previous value. I would recommend please use NSNotificationCenter to post notification when you get a badge which will implement void getter and setter method of badge in platformViewControllerclass. then no instance would be destroy.

Hope this helps

UITabBar, Use the methods of this class to configure the appearance of the tab bar. For tab The UITabBar class and UIToolbar classes have similar appearances but different purposes. Use tab bars to convey and change your app's mode. For more information on when to use both tab bars and toolbars, see iOS Human Interface� Learn about designing apps for iOS. Tab Bars. A tab bar appears at the bottom of an app screen and provides the ability to quickly switch between different sections of an app. Tab bars are translucent, may have a background tint, maintain the same height in all screen orientations, and are hidden when a keyboard is displayed.

I really don't know exactly your problem, but what I think I would use a subclass of nsobject and in each viewController I'd change the badge. Something similar to this: https://stackoverflow.com/a/9736559/2000162

UITabBarItem, You can also specify a badge value on the tab bar item for adding additional This class also provides a number of system defaults for creating items. In iOS v5.0 and later, you can customize the appearance of tab bars by setting item label in a tab bar for selecting between different subtasks, views, or modes in an app. You can also use tab bar items to add a badge to the corresponding tab. For more information about creating and configuring items, see UITab Bar Item. Responding to Tab Selections. For tab bars with an associated tab bar controller, the tab bar controller automatically manages selections and displays the appropriate view controller.

func setBadge(){
        let viewconrollers = self.tabBarController?.viewControllers
        viewconrollers![3].tabBarItem.badgeValue = "Your String value"
        viewconrollers![3].tabBarItem.badgeColor = UIColor.green
    }
//call setBadge() method from view controller's viewdidload method.

// select your give number it the view controllers array for providing badge on the desired tab

How to set a badge value on a UITabBarController item, A UITabBarItem has a badge value which is that little red mark above icon the screenshot above: LIVE is currently the badgeValue of the tab bar item). This approach will work if you have direct access to the class in the view controller. let's create several View Controllers with different colours each… With Tab Bar Item, you will have the option to use built-in title/image or you could customized your own by giving your own title and image. Here we will be using the built in and I have picked Recents, Favorites and More. Also keep in mind to click on each of the Tab Bar Item and change the tag. You’ll notice that it’s all zero.

Starting an iOS Tab Bar App with UITabBarViewController (Updated , The two grey arrows going from the tab bar controller to the other two view So if you were to change the tab icons, you'd first have to add the icon images here in Now this storyboard view will be the view for the UIViewController class you� BackgroundColor: To set the badge background color TextColor: To set the badge text color. public interface IToolbarItemBadgeService { void SetBadge(Page page,ToolbarItem item, string value,Color backgroundColor,Color textColor); } 2-In your Android and iOS projects create a new class and implement the interface already created.

Tabs - Material Design, Tabs can also show a badge (usually a number) like UITabBar. objective-c Conform your class to the MDCTabBarDelegate protocol and set it as the tab bar's the provided shape and animate changes as the user selects different tabs. This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software. Learn more about using Apple's beta software

Change tab bar item badge from other ViewController — Xamarin , Change tab bar item badge from other ViewController iOS. I have a two tab bar items at the bottom of my application. (Let's call the tab bar� These default to the iOS platform defaults, but you can change them here. The color that is passed through to the tabBarIcon is either the active or inactive one, depending on the focused state (focused is active). The size is the size of the icon expected by the tab bar.

Comments
  • Ok. Did you try as I said in my answer?
  • Okay, I get that and it does indeed work if I call the code from any class that the tabbarcontroller is a parent of. But I have this class daemonClass.m that runs a piece of code every 30 seconds that checks for notifications. It is a class-method ( +(void) ). How would I update the badge from this method?
  • Write a method to update the tab bar badge value in the appdelegate and call that method from your daemonclass class method. I mentioned that you can call from any class.(That means any viewcontrollers which is inside the tab bar controller).