Search code examples
iosobjective-cxcodeuinavigationitem

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?


Solution

  • 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];