Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

iPhone: how to badge a TabBatItem?

Hello,
In my AppDelegate.m there is a button and if I click on it, a TabBarItem should get a badge with the value "!". But it should not be the same Item like the View with the button but another TabBarItem.

How can I do this?

In the next step, I will show an alert selecting the the view with the badged Item. The alert should only be displayed if the badge has the value "!".

I hope it's not to much work!?
Thanks for helping me.

MacBook, Mac OS X (10.5.5)

Posted on Nov 23, 2008 12:57 AM

Reply
23 replies

Nov 23, 2008 1:48 AM in response to .macSven

Okay, I figured out that you can use Numbers in a badge.
With the following code it's possible to set the badge of the App-Icon.

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];


If anybody hasn't an idea how I solve my first told problem, maybe you know how I get the Information of the ApplicationBadgeNumber? It would help me, if could use something like this:


if (ApplicationBadgeValue = "1") {
...
}


Any idea?

Nov 23, 2008 4:24 AM in response to iPhil_good

I'am absolutly new in developing with Xcode. The documentation doesn't help me, becuase I don't understand what I have to do.

I've tried something like this:

self.tabBarController.tabBarItem.badgeValue=@"1";


It doesn't work and makes not sense to me, too. Because in the code, there isn't set the specified Item which the badge should display.

It would help me, if you give me some code, please!?

Nov 23, 2008 4:34 AM in response to .macSven

In a UITabBar you have an "items" property which is an NSArray of UITabBarItem.

Knowing the index of your tab, you can access its property through this array :
(UITabBarItem *)([self.tabBarController.items objectAtIntex:index]).badgeValue=@"!";

You may also want to check the selectedItem property of UITabBar if it makes sense in your program.

Is this more helpful ?
Regards
Phil

Nov 23, 2008 4:50 AM in response to iPhil_good

Thanks, this was more helpful, yes. But it doesn't work yet.

I have to put this code in my AppDelegate.h, right?

@property(copy) NSArray *items;

And then I have to sythesize "items" in my AppDelegate.m, right?

When I do this, I get two errors:
1. "request for member "items" in something not a structure or onion"
2. sythesized property "items" must either be named the same as a compatible ivar or must explicitly name an ivar"

Nov 23, 2008 6:51 AM in response to .macSven

Actually 'items' is already a property of your UITabBar instance.
But I made a mistake in the post above considering this was a member of the UITabBarController ;-/

What I would do is to subclass UITabBarController adding a UITabBar *tabBar member, and link the tabbar to this property in Interface Builder. Then you would be able to access the tab bar with :
[self.tabBarController.tabBar.items objectAtIndex:index]

and the badge with :
((UITabBarItem *)([self.tabBarController.tabBar.items objectAtIndex:index]).badgeValue = @"!"

Should be better ! Sorry ...

Phil

Nov 23, 2008 7:23 AM in response to iPhil_good

It seems to be very complicated for me. I have linked my TabBarItem (which should get my badge) to another ViewController. Isn't it possible to put the needed code in my ViewController.h /.m ?

This would be easy I think, because later I want to get the Information if a badge is set - I will make this in my ViewController too.

I know, it's very difficult to help me, but I'am too new in developing. Maybe everyone has solved the problem yet, but I think I need an almost ready code in this case! Sorry!

Nov 23, 2008 9:05 AM in response to iPhil_good

Thank you so much!!!

Here is my AppDelegate.h:


//
// iDesignerAppDelegate.h
// iDesigner
//
// Created by Sven Jansen on 02.11.08.
// Copyright _MyCompanyName_ 2008. All rights reserved.
//
#import <UIKit/UIKit.h>
@class iDesignerViewController;
@class SubViewController;
@interface iDesignerAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate, UITextFieldDelegate, UIActionSheetDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
UINavigationItem *navigationItem;

//für Bildgrößen-Tab
IBOutlet UIView *view;
SubViewController *svController;

IBOutlet UITextField* pxwidth;
IBOutlet UITextField* pxheight;
IBOutlet UITextField* _price;
IBOutlet UITextField* raster;
IBOutlet UITextField* lpi;
IBOutlet UITextField* qf;
IBOutlet UITextField* cmwidth;
IBOutlet UITextField* cmheight;
IBOutlet UIButton* _button;
IBOutlet UIBarButtonItem* _done;
IBOutlet UILabel* info_text;



//für Dateigröße-Tab
IBOutlet UITextField* b_pxwidth;
IBOutlet UITextField* b_pxheight;
IBOutlet UITextField* datentiefe;
IBOutlet UITextField* kompression;
IBOutlet UITextField* kb;
IBOutlet UITextField* mb;
IBOutlet UIButton* datei_button;

//für Scan: DPI-Tab
IBOutlet UITextField* scan_sf;
IBOutlet UITextField* scan_qf;
IBOutlet UITextField* scan_lpi;
IBOutlet UITextField* scan_raster;
IBOutlet UITextField* scan_dpi;
IBOutlet UIButton* scan_button;

NSString *string;

}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, copy) NSString *string;
@property (nonatomic, retain) SubViewController *svController;
@property (nonatomic, retain) IBOutlet UIViewController *viewController;
@property (nonatomic, retain) IBOutlet UINavigationItem *navigationItem;
//@property(copy) NSArray *items;
- (void)updateString;
//für Bildgrößen-Tab
@property (nonatomic, retain) UIView *view;
@property (nonatomic, retain) UITextField *pxwidth;
@property (nonatomic, retain) UITextField *pxheight;
@property (nonatomic, retain) UITextField *raster;
@property (nonatomic, retain) UITextField *lpi;
@property (nonatomic, retain) UITextField *qf;
@property (nonatomic, retain) UITextField *cmwidth;
@property (nonatomic, retain) UITextField *cmheight;
@property (nonatomic, retain) UITextField *_price;
@property (nonatomic, retain) UIButton *_button;
@property (nonatomic, retain) UIBarButtonItem *_done;
//für Dateigröße-Tab
@property (nonatomic, retain) UITextField *b_pxwidth;
@property (nonatomic, retain) UITextField *b_pxheight;
@property (nonatomic, retain) UITextField *datentiefe;
@property (nonatomic, retain) UITextField *kompression;
@property (nonatomic, retain) UITextField *kb;
@property (nonatomic, retain) UITextField *mb;
@property (nonatomic, retain) UIButton *datei_button;
//für Scan: DPI-Tab
@property (nonatomic, retain) UITextField *scan_sf;
@property (nonatomic, retain) UITextField *scan_qf;
@property (nonatomic, retain) UITextField *scan_lpi;
@property (nonatomic, retain) UITextField *scan_raster;
@property (nonatomic, retain) UITextField *scan_dpi;
@property (nonatomic, retain) UIButton *scan_button;
@end


Do you use iChat? I could share my screen, I think it would be the best way isn't it?
Search for svensmacbook as an AIM-Account

Message was edited by: .macSven

Nov 23, 2008 9:37 AM in response to .macSven

I suggest you add an item in your iDesignerAppDelegate class :
IBOutlet UITableBar *tabBar;

along with the traditional @property and @synthesize.

In IB link the new Application Delegate property to the tab bar, and now, after importing "iDesignerAppDelegate.h" in the file you want, you have access to the tab bar items wherever in your code using :
#import "iDesignerAppDelegate.h"
...
iDesignerAppDelegate *ad = (iDesignerAppDelegate *)[[UIApplication sharedApplication] delegate];
UITabBar *tb = ad.tabBar;
UITabBarItem *tbi = [tb.items objectForIndex:index];
//set the badge
tbi.badgeValue = @"!";

I hope this will work this time 😉
Sorry I'm not using any instant messenger ;-/

Nov 23, 2008 9:57 AM in response to iPhil_good

Okay, at first I think I should change the "index" to "4" like this, because my badge should display on item index 4:

tb.items objectForIndex:4


I've put the Code in the .h as you said. Than in IB I selected the Tab Bar and set the connection in "outlets -> delegate" to my .h.

The other code I put in my void which is calling when a button was clicked. I got any error but anything happens!

Where is my misstake? I also tried to set the referencing outlet in IB to *tabBar but when clicked my button my app crashes!

iPhone: how to badge a TabBatItem?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.