我有一个蓝牙应用程序,我希望一旦连接外围设备(自动完成,嵌入在didDiscover
委托中,该应用程序应启动homeViewController
。
为了避免依赖观察者来不断检查外围设备是否成功连接,我打算从centralManager
代表中调用该函数。见下文:
要调用的功能:
class ConnectViewController: UIViewController {
...
func launchHomeScreen() {
guard bluetooth.connected == true else { return }
let rootViewController = UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.rootViewController
guard let mainNavigationController = rootViewController as? MainNavigationController else { return }
mainNavigationController.viewControllers = [HomeViewController()]
self.navigationController?.dismiss(animated: true, completion: nil)
print("Dismissed")
}
}
在蓝牙单例课程中:
...
public func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
ConnectViewController().launchHomeScreen() // Here
delegate?.didConnectedPeripheral?(peripheral)
opsisPeripheral.delegate = self
}
发生的情况是该函数已成功调用,因为调试器显示“ Dismissed”,但HomeViewController
从未显示。
我已经通过使用按钮执行启动功能来进行尝试,并且效果很好...是否有我没有很好遵循的规则?
如果没有,我可以采用其他方法吗?
评论此行
self.navigationController?.dismiss(animated: true, completion: nil)