从 TabBarController 呈现特定的 ViewController
Present a specific ViewController from the TabBarController
我想在触发 本地通知 并执行自定义操作时从 TabBarController
打开特定的 ViewController
。我使用了以下行 code:
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
,而第一个出现并且第二个 notification 被触发:在控制台中:警告尝试呈现 ViewController...
使用这个:
tabBarController.selectedIndex = 1
或者这样:
tabBarController.selectedViewController = tabBarController.viewControllers![1]
其中 1 可以是您的 tabBarController
提供的任何 viewcontrollers
我 运行 遇到了类似的问题,更改 selectedIndex
对我不起作用。根据项目的要求,您可以 实例化 您的 ViewController
并将其添加为 Subview
并移动到那个 Subview
。完成后确保删除 Subview
.
let replyView = self.storyboard?.instantiateViewControllerWithIdentifier("replyView")
self.addChildViewController(replyView!)
self.view.addSubview(replyView!.view)
replyView!.didMoveToParentViewController(self)
我想在触发 本地通知 并执行自定义操作时从 TabBarController
打开特定的 ViewController
。我使用了以下行 code:
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
,而第一个出现并且第二个 notification 被触发:在控制台中:警告尝试呈现 ViewController...
使用这个:
tabBarController.selectedIndex = 1
或者这样:
tabBarController.selectedViewController = tabBarController.viewControllers![1]
其中 1 可以是您的 tabBarController
viewcontrollers
我 运行 遇到了类似的问题,更改 selectedIndex
对我不起作用。根据项目的要求,您可以 实例化 您的 ViewController
并将其添加为 Subview
并移动到那个 Subview
。完成后确保删除 Subview
.
let replyView = self.storyboard?.instantiateViewControllerWithIdentifier("replyView")
self.addChildViewController(replyView!)
self.view.addSubview(replyView!.view)
replyView!.didMoveToParentViewController(self)