2

I have two views A & B in order to go from A to B I'm just pushing the new view, in view B I preform some actions that update the badgeValue of a UIBarButton item of my navigation bar. (Like adding an item to the shopping cart)

picture 30

When I go back to view A using the navigationBar Back button the UIBarButton item shows the previous badgeValue, instead of the updated one, I guess this happens because when you press the back button view B is popped revealing view A which is left as it was without any changes so, I need a way to tell view A to update itself after returning.

picture 29

If instead of going back to view A I go back to the rootViewController the badgeValue is updated, I guess that when I go to the rootViewController the code in viewDidLoad is read and the badgeValue is updated.

Any ideas on how to update a view that was behind another that was just popped?

EDIT

@silicon_valley answer is correct but it won't work with MikeMTOL's library because it's buggy, for Swift users I'd recommend these extensions instead. -> link

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Octavio Rojas
  • 187
  • 13

1 Answers1

3

You can add your code to update the badge value in viewWillAppear. That will make sure the value is updated every time the view will come back on the screen (and not just the first time it is created).

silicon_valley
  • 2,639
  • 2
  • 16
  • 20
  • I tried to do what you said, but what this does is that it disappears the badgeValue completely. I'm currently updating the badgeValue in the viewDidLayoutSubViews() method: `override func viewDidLayoutSubviews() { var numeroProducto = String(Carrito.numProd) navigationItem.rightBarButtonItem?.badgeValue = numeroProducto }` that's the only way I can keep it when pushing to another viewController because if I put it in viewDidLoad it disappears after the new controller is pushed. – Octavio Rojas Jan 11 '17 at 22:09
  • So how/where are you creating the `UIBarButtonItem`? The behavior you describe seems odd... – silicon_valley Jan 11 '17 at 22:21
  • I'm not creating it, it's just the standard back button arrow that the navigation bar has, the only thing I did was change it's default icon for a custom one and I'm using a single navigation controller for all the views after the login view. I'm using the standard back button because I don't want to create a button for all my views and have to use a selector for each in order to perform the back function, which also "pops" the viewcontroller on top. – Octavio Rojas Jan 11 '17 at 22:25
  • I'm not sure I understand what you're saying. The `rightBarButtonItem` seems like a custom `UIBarButtonItem` if it's got a property called `badgeValue`? I don't mean the back button but the button with the shopping cart icon. Or are they the same? – silicon_valley Jan 11 '17 at 22:30
  • Oh sorry I thought you were talking about the back button. The `rightBarButtonItem` is being created with the Interfase Builder but I'm using a library called `UIBarButtonItem + Badge` - [link](https://github.com/mikeMTOL/UIBarButtonItem-Badge), it basically expands the functionality of the `BarButtons` or `UIButtons` with the ability to put a `badgeValue` in them like this `self.navigationItem.leftBarButtonItem.badgeValue = "1"` I'm saving an array of items and then I load them back when the app starts I get the badgeValue using the array.count – Octavio Rojas Jan 11 '17 at 22:38
  • Very strange. I've just created my own view controller which works fine: `class ViewController2: UIViewController { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) if let rightbbi = navigationItem.rightBarButtonItem { rightbbi.badgeValue = "\(arc4random_uniform(20))" } else { navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .search, target: nil, action: nil) } } }` – silicon_valley Jan 11 '17 at 22:57
  • Ok, now try this, in your first viewcontroller assign any value (not random or it won't work) create another viewcontroller and in that viewcontroller add +1 to your badgeValue, then press the back button on the navigation bar you'll get back to view A and the badgeValue that you'll have will be the previous one, not the updated value with +1 I found a fork of that class - [link](https://github.com/MobileXLabs/UIBarButtonItem-Badge/), I hope this one works better for me but you need to install the KVOController libraries to use it, I'm downloading them and i'll let you know. – Octavio Rojas Jan 11 '17 at 23:19
  • Okay, yes, now I have the same problem. It looks like a bug in the UIBarButtonItem+Badge code. – silicon_valley Jan 11 '17 at 23:34
  • 1
    I Finally solved it! The solution above works but you need to change MikeMTOL's UIBarButtonItems+Badge for another library because it has a bug that prevents you to update the badge value in `viewWillAppear` The solution provided above + this gist -> [link](https://gist.github.com/yonat/75a0f432d791165b1fd6) worked like a charm for me it's a bit more complicated to setup a button vs MikeMTOL's but this one works well, and I searched and searched for badgeValue classes or frameworks and the one of the link above provided to be the best. – Octavio Rojas Jan 12 '17 at 17:12