I have a TypingController which when certain key are pressed will send out notifications. A SwiftUI view will receive those notifications and do something with them.
I am trying to figure out how to add this view controller to SceneDelegate.swift to maybe set this as the window.rootViewController but I am unable to work out how.
Any solutions to this would be appreciated.
Here's my code:
I am trying to figure out how to add this view controller to SceneDelegate.swift to maybe set this as the window.rootViewController but I am unable to work out how.
Any solutions to this would be appreciated.
Here's my code:
Code Block Swift extension Notification.Name { static let enter = Notification.Name("enter") static let remove = Notification.Name("remove") static let submit = Notification.Name("submit") } class TypingController: UIViewController { override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) { guard let key = presses.first?.key else { return } switch key.keyCode { case .keyboardDeleteOrBackspace: NotificationCenter.default.post(name: .remove, object: nil) case .keyboardReturn: NotificationCenter.default.post(name: .submit, object: nil) default: guard let characters = key.characters else { return } if let number = Int(characters) { NotificationCenter.default.post(name: .enter, object: number) } else { super.pressesBegan(presses, with: event) } } } }
I have solved this now.
I added this method to the TypingController.
and changed the line in SceneDelegate.swift to window.rootViewController = TypingController().
I added this method to the TypingController.
Code Block Swift override func viewDidLoad() { super.viewDidLoad() let hostingController = UIHostingController(rootView: ContentView()) view.addSubview(hostingController.view) }
and changed the line in SceneDelegate.swift to window.rootViewController = TypingController().