Я хочу, чтобы открыть определенный ViewController
из TabBarController
всякий раз, когда местное уведомление обжигают и их пользовательское действие выполняется. Я использовал следующую строку из кода:Представить конкретную ViewController из TabBarController
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
switch response.actionIdentifier {
case "first":
DispatchQueue.main.async(execute: {
self.first()
})
case "second":
DispatchQueue.main.async(execute: {
self.second()
})
default:
break
}
completionHandler()
}
Так что это first()
функция:
func first() {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController
let navigationController = storyboard.instantiateViewController(withIdentifier: "First") as! UINavigationController
tabBarController.present(navigationController, animated: true) {
}
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.tintColor = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
}
Вторая функция: second()
func second() {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController
let navigationController = storyboard.instantiateViewController(withIdentifier: "Second") as! UINavigationController
tabBarController.present(navigationController, animated: true) {
}
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.tintColor = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
}
И это хорошо работает, но я не могу открыть второй ViewController
в то время как первый из них представлен и второй уведомления обжигает: В консоли: Предупреждение попытки представить ViewController ...
Это кажется бесполезным. Он возвращается только 1 – Mannopson
Ему ничего не нужно возвращать. Если вы установите значение selectedIndex на любое число, панель вкладок изменит вкладку на выбранный индекс. Если вы установите selectedIndex = 0, вы переключитесь на первую вкладку. selectedIndex = 1, вторая вкладка. И так далее –
Спасибо за помощь. У меня нет опыта работы с классом UITabBarController. – Mannopson