turbolinks / turbolinks-ios

Native iOS adapter for building hybrid apps with Turbolinks 5
MIT License
880 stars 92 forks source link

Working with native controls #101

Closed reinink closed 7 years ago

reinink commented 7 years ago

Still getting familiar with Turbolinks iOS, and I've run into a situation where I'm using a native tab bar controller to create a tabbed menu in my app. I'm set it up so that when you click on a link in that tabbed menu, it loads the corresponding controller, and that controller makes a page request. Something like this:

import Turbolinks
import UIKit

class MessagesViewController: ApplicationController {

    override var url: URL {
        return URL(string: "http://example.com/messages")!
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        presentVisitableForSession(url: url)
    }
}

The issue I'm running into is with the titles in the native tab bar. They use the controller name, which Turbolinks automatically sets (I'm assuming) to the current page <title>. This is creating weird results. For example, if I go to a "Messages" tab, and it loads my latest message, the tab now shows the title of that message:

Before: image

After clicking "Messages": image

Would you recommend using native controls in this way? Or am I maybe going too far with them? And if this approach is fine, any thoughts on how to prevent the menu item titles from changing?

Thanks!!!

zachwaugh commented 7 years ago

We use many native controls in the Basecamp 3 app, which also uses Turbolinks. Each tab of the UITabBarController has it's own UINavigationController. On the UINavigationController, we explicitly set the tabBarItem property which prevents the children view controllers from changing the title. You can also implement your own VisitableViewController and not set the viewController.title

henrik commented 6 years ago

@zachwaugh Could you expand a bit on

In the UINavigationController, we explicitly set the tabBarItem property which prevents the children view controllers from changing the title.

We've tried setting tabBarItem.title = "Foo" in viewDidLoad in the nav controller, but Turbolinks replaces that title. What are we missing?

henrik commented 6 years ago

We solved this by making our own VisitableViewController subclass:

import Turbolinks

class CustomVisitableViewController: VisitableViewController {
    override func visitableDidRender() {
        // So the tab bar item text doesn't change to the web view's title.
        navigationItem.title = visitableView.webView?.title
    }
}

And we made sure to use CustomVisitableViewController instead of VisitableViewController in our code.