我可以在一个应用程序中混合使用UIKit和TVMLKit吗? [英] Can I mix UIKit and TVMLKit within one app?
问题描述
我正在探索tvOS
,我发现Apple提供了一套不错的TVML编写的>模板.我想知道使用TVML
模板的tvOS
应用程序是否也可以使用UIKit
.
我可以在一个应用程序中混合使用UIKit和TVMLKit吗?
我在 Apple开发者论坛上找到了一个主题,但是它没有完全回答这个问题,我正在经历文档以找到答案.
是的,可以.显示TVML模板要求您使用控制JavaScript上下文的对象: TVApplicationController .
var appController: TVApplicationController?
此对象具有与之关联的 UINavigationController 属性.因此,只要您认为合适,就可以致电:
let myViewController = UIViewController()
self.appController?.navigationController.pushViewController(myViewController, animated: true)
这使您可以将Custom UIKit viewcontroller推入导航堆栈.如果要返回TVML模板,只需将viewController弹出导航堆栈即可.
如果您想知道如何在JavaScript和Swift之间进行通信,请使用以下方法来创建名为 pushMyView()
的javascript函数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(),它将视图控制器推入堆栈./p>
SWIFT 4.1更新
对方法名称和类型转换进行一些简单的更改:
appController?.evaluate(inJavaScriptContext: {(evaluation: JSContext) -> Void in
和
evaluation.setObject(unsafeBitCast(pushMyViewBlock, to: AnyObject.self), forKeyedSubscript: "pushMyView" as NSString)
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
.
Can I mix UIKit and TVMLKit within one app?
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.
Yes, you can. Displaying TVML templates requires you to use an object that controls the JavaScript Context: TVApplicationController.
var appController: TVApplicationController?
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)
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.
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
})
}
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 UPDATE
Just a few simple changes to method names and casting:
appController?.evaluate(inJavaScriptContext: {(evaluation: JSContext) -> Void in
and
evaluation.setObject(unsafeBitCast(pushMyViewBlock, to: AnyObject.self), forKeyedSubscript: "pushMyView" as NSString)
这篇关于我可以在一个应用程序中混合使用UIKit和TVMLKit吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!