我对 GCController 框架有疑问:
我有 2 个视图控制器,每个视图控制器都有一个按钮来加载另一个视图控制器。
第一个控制器代码:
import Cocoa
import GameController
class ViewController: NSViewController {
@IBAction func abirVc2(_ sender: Any) {
if let controller = self.storyboard?.instantiateController(withIdentifier: "ViewController2") as? ViewController2 {
self.view.window?.contentViewController = controller
}
}
override func viewDidLoad() {
super.viewDidLoad()
startWatchingForControllers()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
func startWatchingForControllers() {
// Subscribe for the notes
let ctr = NotificationCenter.default
ctr.addObserver(forName: .GCControllerDidConnect, object: nil, queue: .main) { note in
if let ctrl = note.object as? GCController {
self.add(ctrl)
}
}
ctr.addObserver(forName: .GCControllerDidDisconnect, object: nil, queue: .main) { note in
if let ctrl = note.object as? GCController {
self.remove(ctrl)
}
}
// and kick off discovery
GCController.startWirelessControllerDiscovery(completionHandler: {})
}
func stopWatchingForControllers() {
GCController.stopWirelessControllerDiscovery()
let ctr = NotificationCenter.default
ctr.removeObserver(self, name: .GCControllerDidConnect, object: nil)
ctr.removeObserver(self, name: .GCControllerDidDisconnect, object: nil)
}
func add(_ controller: GCController) {
let name = String(describing:controller.vendorName)
if let extendedGamepad = controller.extendedGamepad {
print("connect extended \(name)")
configureDPadButtons(extendedGamepad)
//configureDiamondButtons(extendedGamepad)
//configureShoulderButtons(extendedGamepad)
//configureTriggers(extendedGamepad)
return
} else if let gamepad = controller.microGamepad {
print("connect micro (name)")
} else {
print("Huh? (name)")
}
}
private func configureDPadButtons(_ gamepad: GCExtendedGamepad) {
gamepad.dpad.up.pressedChangedHandler = {(button, value, pressed) in
if let controller = self.storyboard?.instantiateController(withIdentifier: "ViewController2") as? ViewController2 {
self.view.window?.contentViewController = controller
}
}
gamepad.dpad.down.pressedChangedHandler = {(button, value, pressed) in
print("ExtendedGamepad - Down")
}
gamepad.dpad.left.pressedChangedHandler = {(button, value, pressed) in
print("ExtendedGamepad - Left")
}
gamepad.dpad.right.pressedChangedHandler = {(button, value, pressed) in
print("ExtendedGamepad - Right")
}
}
func remove(_ controller: GCController) {
}
}
第二个控制器代码:
import Cocoa
class ViewController2: NSViewController {
@IBAction func abrirHomeView(_ sender: Any) {
if let controller = self.storyboard?.instantiateController(withIdentifier: "HomeView") as? ViewController {
self.view.window?.contentViewController = controller
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
在第一个控制器上,我按下按钮加载第二个,然后按下第二个视图控制器的按钮返回到第一个。每次我按下按钮时它都会起作用。
然后我使用 GamePad 的向上箭头加载 viewController 2,它正确加载,然后我使用第二个 viewcontroller 上的按钮返回到第一个控制器,好的,它也可以,但是如果我使用 GamePad 的向上按钮来加载第二个控制器再次,它没有做任何事情......
似乎处理 GamePad 向上箭头的函数无法再次加载第二个 viewController。
我使用了一个简单的项目来进行测试,就这么简单,2 个视图控制器,每个控制器都有一个按钮......
我不知道发生了什么...
提前致谢