Je veux ouvrir un ViewController
spécifique de la TabBarController
chaque fois qu'une notification locale est tiré et leur action personnalisée est effectuée. Je l'ai utilisé la ligne suivante de Code:Présenter un ViewController spécifique du 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()
}
Il est donc fonction 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()
}
fonction Deuxième : 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()
}
Et ça fonctionne bien, mais je ne peux pas ouvrir le deuxième ViewController
alors que le premier est présenté et la deuxième notification est tiré: Dans la console: tentative d'avertissement pour présenter ViewController ...
Cela semble inutile. Il retourne seulement 1 – Mannopson
Il n'a rien à retourner. Si vous définissez selectedIndex sur n'importe quel nombre, la barre d'onglets changera son onglet en index sélectionné. Si vous définissez selectedIndex = 0, vous passerez au premier onglet. selectedIndex = 1, deuxième onglet. Et ainsi de suite –
Merci pour votre aide. Je n'ai aucune expérience avec la classe UITabBarController. – Mannopson