首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示来自TabBarController的特定TabBarController

显示来自TabBarController的特定TabBarController
EN

Stack Overflow用户
提问于 2017-02-15 02:59:24
回答 2查看 704关注 0票数 0

每当触发本地通知并执行它们的自定义操作时,我都希望从TabBarController打开一个特定的TabBarController。我使用了下面的代码行

代码语言:javascript
复制
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()函数

代码语言:javascript
复制
 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()

代码语言:javascript
复制
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.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-15 03:01:32

用这个:

代码语言:javascript
复制
tabBarController.selectedIndex = 1

或者这个:

代码语言:javascript
复制
tabBarController.selectedViewController = tabBarController.viewControllers![1]

其中1可以是由tabBarController表示的任何viewcontrollers

票数 2
EN

Stack Overflow用户

发布于 2017-02-15 03:15:27

我遇到了一个类似的问题,改变selectedIndex对我不起作用。根据项目的需求,您可以实例化ViewController并将其添加为Subview并移动到该Subview。完成后,请确保删除该Subview

代码语言:javascript
复制
let replyView = self.storyboard?.instantiateViewControllerWithIdentifier("replyView")
    self.addChildViewController(replyView!)
    self.view.addSubview(replyView!.view)
    replyView!.didMoveToParentViewController(self)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42239917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档