我可以在一个应用程序中混合使用 UIKit 和 TVMLKit 吗? [英] Can I mix UIKit and TVMLKit within one app?
问题描述
我正在探索 tvOS
,发现 Apple 提供了一套不错的 模板 使用 TVML
编写.我想知道使用 TVML
模板的 tvOS
应用程序是否也可以使用 UIKit
.
I'm exploring tvOS
and I found that Apple offers nice set of templates written using TVML
. I'd like to know if a tvOS
app that utilises TVML
templates can also use UIKit
.
我可以在一个应用中混合使用 UIKit 和 TVMLKit 吗?
我在 Apple 开发者论坛 上找到了一个帖子,但它并没有完全回答这个问题,我正在处理文档以找到答案.
I found a thread on Apple Developer Forum but it does not fully answer this question and I am going through documentation to find an answer.
推荐答案
是的,你可以.显示 TVML 模板需要您使用控制 JavaScript 上下文的对象:TVApplicationController.
Yes, you can. Displaying TVML templates requires you to use an object that controls the JavaScript Context: TVApplicationController.
var appController: TVApplicationController?
这个对象有一个与之关联的 UINavigationController 属性.因此,只要您认为合适,您就可以致电:
This object has a UINavigationController property associated with it. So whenever you see fit, you can call:
let myViewController = UIViewController()
self.appController?.navigationController.pushViewController(myViewController, animated: true)
这允许您将自定义 UIKit 视图控制器推送到导航堆栈上.如果您想返回 TVML 模板,只需将 viewController 从导航堆栈中弹出即可.
This allows you to push a Custom UIKit viewcontroller onto the navigation stack. If you want to go back to TVML Templates, just pop the viewController off of the navigation stack.
如果你想知道如何在 JavaScript 和 Swift 之间进行通信,这里有一个方法可以创建一个名为 pushMyView()
If what you would like to know is how to communicate between JavaScript and Swift, here is a method that creates a javascript function called pushMyView()
func createPushMyView(){
//allows us to access the javascript context
appController?.evaluateInJavaScriptContext({(evaluation: JSContext) -> Void in
//this is the block that will be called when javascript calls pushMyView()
let pushMyViewBlock : @convention(block) () -> Void = {
() -> Void in
//pushes a UIKit view controller onto the navigation stack
let myViewController = UIViewController()
self.appController?.navigationController.pushViewController(myViewController, animated: true)
}
//this creates a function in the javascript context called "pushMyView".
//calling pushMyView() in javascript will call the block we created above.
evaluation.setObject(unsafeBitCast(pushMyViewBlock, AnyObject.self), forKeyedSubscript: "pushMyView")
}, completion: {(Bool) -> Void in
//done running the script
})
}
在 Swift 中调用 createPushMyView() 后,您可以在 javascript 代码中自由调用 pushMyView(),它会将视图控制器推送到堆栈中.
Once you call createPushMyView() in Swift, you are free to call pushMyView() in your javascript code and it will push a view controller onto the stack.
SWIFT 4.1 更新
只需对方法名称和转换进行一些简单的更改:
Just a few simple changes to method names and casting:
appController?.evaluate(inJavaScriptContext: {(evaluation: JSContext) -> Void in
和
evaluation.setObject(unsafeBitCast(pushMyViewBlock, to: AnyObject.self), forKeyedSubscript: "pushMyView" as NSString)
这篇关于我可以在一个应用程序中混合使用 UIKit 和 TVMLKit 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!