Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

ios - 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.

I'd be grateful for any insight. If anyone knows how to programmatically update the selected tab of a CPTabBarTemplate that would be dandy.

Thanks for reading!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...