问题描述
是否可以在 tvOS 应用中同时使用本机视图/控制器和 TVML?似乎使用 TVML 需要您设置应用程序控制器"而不是普通的视图控制器.如何在 tvOS 应用中同时使用 TVML 和原生组件?
Is it possible to use both native views/controllers and TVML in a tvOS app? It seems that using TVML requires you to set an "app controller" instead of a normal view controller. How can you use both TVML and native components in a tvOS app?
推荐答案
是的,您可以在同一个应用中同时使用 Native 和 TVML.您可以在 App Delegate 的evaluateAppJavaScriptInContext 方法中注册您的类对象.
Yes, you can use both Native and TVML in the same app.You can register your class object in evaluateAppJavaScriptInContext method of App Delegate.
func appController(appController: TVApplicationController, evaluateAppJavaScriptInContext jsContext: JSContext)
{
jsContext.setObject(TestClass(), forKeyedSubscript: "testClassObj")
}
您的 TestClass 应采用 TestClassExport 协议.(我的 TestClass 在 Objective C 中.你也可以用 Swift 编写.)
Your TestClass should adopt the TestClassExport Protocol. (My TestClass is in Objective C. you can write it in Swift also.)
@protocol TestClassExport <JSExport>
- (NSString*)log:(NSString*)string;
@end
@interface TestClass : NSObject <TestClassExport>
-(NSString*)log:(NSString*)string;
@end
现在您可以从 Javascript 调用 Test Class Log 方法:
Now you can call the Test Class Log method from Javascript:
testClassObj.log('来自 JS 的调用');
如果你想显示任何控制器,那么你可以实现将控制器推送到 TVApplicationController 中的方法.
If you want to display any controller then you can implement the method which will push the Controller in the TVApplicationController.
[_tvAppController.navigationController pushViewController:controller animated:YES completion:nil];
查看此链接了解更多详情-https://forums.developer.apple.com/thread/18430
Check this link for more details-https://forums.developer.apple.com/thread/18430
这篇关于如何在 tvOS 应用中同时使用原生和 TVML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!