The Hotwire Native iOS guide shows how to add a single button to the navigation bar. However, what if I want more than one button? (Like, maybe 2 or 3, not trying to get too crazy with buttons.) I have found that you can replace setting rightBarButtonItem with appending to rightBarButtonItems, e.g.
- viewController.navigationItem.rightBarButtonItem = item
+ viewController.navigationItem.rightBarButtonItems?.append(item)
But when I do this, I end up with very strange behavior after navigations. It seems that when I navigate (click a link) and then go back, sometimes I will get an additional set of buttons added, and it keeps repeating on each navigation back to what I am guessing is a cached view. Also, sometimes all the buttons disappear after clicking a link, even though the web element that’s connected to the native Bridge component is on the new page as well as the old page.
One trick I’ve found is that if I keep the label of the button unique, I can avoid adding a duplicate button by checking to see if a button with the same name is already in rightBarButtonItems
. I don’t know if this will fully work though.
To me, what might be nice is if there was a way for me to reset the rightBarButtonItems
after each navigation action. It seems somewhat surprising to me that it doesn’t clear but I guess that would be assuming something about how people are using that property. I am a Swift/iOS/Mac newb so I’m not sure how to go about this. I’m wondering if I can use an extension
to kind of monkey-patch HotwireWebViewController
or something but I’m not sure if that would be the right approach. I noticed in this issue someone subclassed it instead. That code seems to come from the demo app, but the guide for native screens has a slightly different structure, where the default case is just .accept
, and I don’t know whether one way is preferred over the other.
In general when the demo app differs from the guides I’m not sure which one is more “up to date”. But maybe in this case both achieve the same thing and it just depends on if you want to customize the controller. I just don’t want to miss some kind of default behavior from updates or something given that I know very little about this.