iosswiftcarplay

Programmatically change selectedTemplate of CPTabBarTemplate


I'm trying to learn more about CarPlay. Specifically using CPTabBarTemplate (a new template as of 2020 and iOS 14).
What I'm looking to do is programmatically change the selectedTemplate, just as if I were updating a selectedTab in a UITabBar.
An example flow would be a CPTabBarTemplate with two tabs. A CPPointOfInterestTemplate and a CPInformationTemplate.

tabBarTemplate = CPTabBarTemplate(templates: [pointOfInterestTemplate, informationTemplate])

The user could select a point of interest and from the poi detail screen choose a button that might say "Select", just like in Apple's example from their WWDC20 talk. enter image description here

Upon pressing "Select", I want to programmatically take the user to the informationTemplate of the tabBarTemplate and then use the selected location's details to populate information in the informationTemplate.

I might not be understanding the way Apple expects CPTabBarTemplate to be used. There does exist a selectedTab property of CPTabBarTemplate. However, that property is get only. Which leads me to believe that the desired way to switch tabs is let the user tap the next tab themselves.

How does one programmatically update the selected tab of a CPTabBarTemplate?

Thanks for reading!


Solution

  • I don't think this is currently possible. The public interface of CPTabBarTemplate is very short but I couldn't even get it working by calling private api. Neither calling tabTemplate.perform(Selector(("setIndexOfSelectedTab:")), with: 1) nor the invoking function tabTemplate.perform(Selector(("handleActionForControlIdentifier:")), with: tabTemplates[1].value(forKey: "identifier")) made the trick (even though the tab bar delegate function is called with the correct selected template).