每当触发本地通知并执行它们的自定义操作时,我都希望从TabBarController打开一个特定的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.
发布于 2017-02-15 03:01:32
用这个:
tabBarController.selectedIndex = 1或者这个:
tabBarController.selectedViewController = tabBarController.viewControllers![1]其中1可以是由tabBarController表示的任何viewcontrollers
发布于 2017-02-15 03:15:27
我遇到了一个类似的问题,改变selectedIndex对我不起作用。根据项目的需求,您可以实例化ViewController并将其添加为Subview并移动到该Subview。完成后,请确保删除该Subview。
let replyView = self.storyboard?.instantiateViewControllerWithIdentifier("replyView")
self.addChildViewController(replyView!)
self.view.addSubview(replyView!.view)
replyView!.didMoveToParentViewController(self)https://stackoverflow.com/questions/42239917
复制相似问题