Mihir Oza
Mihir Oza

Reputation: 2796

How to reload UIBarButtonItem in IOS

My app is like a shopping cart. I have added one cart UIBarButtonItem in navigation bar.
In cart button I have added a badge at top.
Now my question is when I add an Item on cart How to update badge on button without navigation or when I delete an item from cart badge would be update.
Is it possible to reload navigation bar without navigation?

Upvotes: 3

Views: 1201

Answers (2)

Mihir Oza
Mihir Oza

Reputation: 2796

I just Update my badge when I want to reload. For e.g. when I delete any product from cart I reinitialise the bar button item. Here, is my code:

 NSMutableArray *arrProducts = [[notification userInfo] objectForKey:@"CartList"];

[[NSUserDefaults standardUserDefaults] setInteger:[arrProducts count] forKey:@"Badge"];
[[NSUserDefaults standardUserDefaults] synchronize];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backButtonImage = [UIImage imageNamed:@"shopping_cart.png"];
[button setBackgroundImage:backButtonImage forState:UIControlStateNormal];
// add badge to cart button.
[CommonUtils AddBadge:button];
[button addTarget:self action:@selector(AddToCartClicked:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 30, 30);

[btnAddtoCart initWithCustomView:button];

Upvotes: 1

Kumar
Kumar

Reputation: 1942

Check condition and show/hide badge accordingly.

First, you have to hide badge. When any item is selected check count of item selected is greater than 0 then show badge.

Every time any item selected show badge.

Every time any item deselected check if selected item == 0 then hide badge.

Upvotes: 0

Related Questions