【问题标题】:How to reload UIBarButtonItem in IOS如何在IOS中重新加载UIBarButtonItem
【发布时间】:2015-07-17 06:31:59
【问题描述】:

我的应用就像一个购物车。我在导航栏中添加了一个购物车 UIBarButtonItem。
在购物车按钮中,我在顶部添加了一个徽章。
现在我的问题是,当我在购物车上添加商品时,如何在没有导航的情况下更新按钮上的徽章,或者当我从购物车中删除商品时,徽章会被更新。
是否可以在没有导航的情况下重新加载导航栏?强>

【问题讨论】:

  • github.com/matteogobbi/UIView-MGBadgeView 这可能对你有帮助。
  • 我已经添加了cutombadge。但我的问题是更改徽章时如何重新加载?
  • 如果你使用过 MGBadgeview 那么你可以使用 [btnAddToCart.badgeView setBadgeValue:[arrOrder count]];
  • 但我是另一个第三方老兄。 github.com/ckteebe/CustomBadge
  • 里面有一种方法,我认为这个方法可以在徽章上设置文本 badge3.badgeText = @"4";

标签: ios objective-c xcode uinavigationitem


【解决方案1】:

我只是在我想重新加载时更新我的​​徽章。例如当我从购物车中删除任何产品时,我会重新初始化栏按钮项目。这是我的代码:

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

【讨论】:

  • 我宁愿建议您维护要添加到 UIBarButtonItem 的 UIButton 的全局实例。然后可以使用 UIButton 的全局实例来更新 bagde。因此可以避免完全创建整个按钮。
【解决方案2】:

检查条件并相应地显示/隐藏徽章。

首先,您必须隐藏徽章。选择任何项目时,选中所选项目的数量大于0然后显示徽章。

每次选择任何项目时都会显示徽章。

每次取消选择任何项目时,检查选定项目是否 == 0,然后隐藏徽章。

【讨论】:

    猜你喜欢
    • 2021-01-20
    • 2012-04-08
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    相关资源
    最近更新 更多