HI
lua Corona Can not trigger event to Swift
Xcode 8.2.1
Swift Code
import UIKit
class ViewController: UIViewController , CoronaViewDelegate{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let coronaController = CoronaViewController() // Step 1
self.addChildViewController(coronaController) // Step 2
let view:CoronaView = coronaController.view as! CoronaView
view.frame = self.view.frame // Step 3
self.view.addSubview(view) // Step 4
view.run(); // Step 5
let event = [“name”:“nativeMessage”, “phase”:“loaded”, “message”:“Hello from Swift!”]
view.sendEvent(event)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func coronaView(view: CoronaView, receiveEvent event: NSDictionary) -> AnyObject? {
print(“receiveEvent (event)”)
return(“Nice to meet you!” as AnyObject?)
}
}
lua Code:
local function handleTap( event )
print( ‘hello swift’ )
local data = { name=“coronaView”, message={} }
Runtime:dispatchEvent( data )
return true
end
local startText = display.newText( “START”, display.contentCenterX, 200, “HelveticaNeue”, 24 )
startText:addEventListener( “tap”, handleTap )
–
– Set up a listener to handle return event from Swift
–
local message = display.newText( “”, display.contentCenterX, 25, “HelveticaNeue”, 16 )
local function handleNativeMessage( event )
if ( event.phase == “loaded” ) then
print( ‘corona:’ … event.name ) – “nativeMessage”
print( ‘corona:’ … event.message ) – “Hello from Swift!”
message.text = event.message
end
return true
end