在 AppDelegate 中打开 ViewController 同时保持 Tabbar [英] Opening ViewController In AppDelegate While Keeping Tabbar

查看:23
本文介绍了在 AppDelegate 中打开 ViewController 同时保持 Tabbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Xcode 项目中,当用户点击通知时,我想首先将它们发送到我的 tabBar 中的某个项目,然后我想实例化一个视图控制器并将一个对象发送到该视图控制器.我有将它们发送到我想要的 tabBar 的代码,但我不知道如何在保持 tabBar 和导航栏连接到视图控制器的同时将它们实例化到视图控制器.对此的所有答案都要求您更改根视图控制器,这会使我在调用视图控制器时失去与 tabBar 和导航栏的连接.

In my Xcode project when a user taps on a notification I want to first send them to a certain item in my tabBar then I want to instantiate a view controller and send an object over to that view controller. I have code the that sends them to the tabBar I want, but I do not know how to instantiate them to the view controller while keeping the tabBar and navigation bar connected to the view controller. All the answers on this require you to change the root view controller and that makes me lose connection to my tabBar and navigation bar when the view controller is called.

一个真实的例子: 用户收到 Instagram 通知说John 开始关注你" -> 用户点击通知 -> Instagram 打开并显示通知选项卡 -> 快速将用户发送到John" 配置文件,当用户按下后退按钮时,它会将它们发送回通知选项卡

A Real Life Example of this: User receives Instagram notification saying "John started following you" -> user taps on notification -> Instagram opens and shows notifications tab -> quickly send user to "John" profile and when the user presses the back button, it sends them back to the notification tab

应该知道:我之所以先去某个标签是为了得到那个标签的导航控制器,因为我要去的视图控制器没有.

这是我将用户发送到通知"选项卡的工作代码(我添加了评论以表现得像 Instagram 示例以便更好地理解):

Here's my working code on sending the user to "notifications" tab (I added comments to act like the Instagram example for better understanding):

if let tabbarController = self.window!.rootViewController as? UITabBarController {
    tabbarController.selectedViewController = tabbarController.viewControllers?[3] //goes to notifications tab
    if type == "follow" { //someone started following current user                            
        //send to user's profile and send the user's id so the app can find all the information of the user                    
    }
}

推荐答案

首先,你要满足一个 TabBarController:

First of all, you'll to insatiate a TabBarController:

let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController

然后对 TabBarController 的所有 viewControllers 感到厌烦.如果您的 viewControllers 嵌入到 UINavigationController 中?如果是这样,您将不得不使用导航控制器:

And then insatiate all of the viewControllers of TabBarController. If your viewControllers is embedded in to the UINavigationController? If so, you'll to insatiate a Navigation Controller instead:

let first = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let second = storyboard.instantiateViewiController(withIdentifier: "YourSecondNavigationController") as! UINavigationController
let third = storyboard.instantiateViewiController(withIdentifier: "YourThirdNavigationController") as! UINavigationController

你也应该实例化你想要的 ViewController:

Also you should instantiate your desired ViewController too:

let desiredVC = storyboard.instantiateViewController(withIdentifier: "desiredVC") as! ExampleDesiredViewController

将所有的 NavigationControllers 设为 TabBarController 的 viewControllers:

Make all of the NavigationControllers as viewControllers of TabBarController:

tabBarController.viewControllers = [first, second, third]

并检查:这取决于您的选择.

And check: It's about your choice.

if tabBarController.selectedViewController == first {

// Option 1: If you want to present
first.present(desiredVC, animated: true, completion: nil)

// Option 2: If you want to push
first.pushViewController(desiredVC, animated. true)

}

将 tabBarController 设为 rootViewController:

Make tabBarController as a rootViewController:

self.window = UIWindow.init(frame: UIScreen.main.bounds)   
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()

最后:这是你完成的代码:

Finally: It's your completed code:

func openViewController() {

let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController

let first = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let second = storyboard.instantiateViewiController(withIdentifier: "YourSecondNavigationController") as! UINavigationController
let third = storyboard.instantiateViewiController(withIdentifier: "YourThirdNavigationController") as! UINavigationController

let desiredVC = storyboard.instantiateViewController(withIdentifier: "desiredVC") as! ExampleDesiredViewController

tabBarController.viewControllers = [first, second, third]

if tabBarController.selectedViewController == first {

// Option 1: If you want to present
first.present(desiredVC, animated: true, completion: nil)

// Option 2: If you want to push
first.pushViewController(desiredVC, animated. true)

}

self.window = UIWindow.init(frame: UIScreen.main.bounds)   
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()

}

如果您想在点击通知时呈现或推送 ViewController?尝试这样的事情:

If you want to present or push ViewController when the notification is tapped? Try something like that:

extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        switch response.actionIdentifier {
        case UNNotificationDefaultActionIdentifier:
            openViewController()
            completionHandler()

        default:
            break;
        }
    }
}

这篇关于在 AppDelegate 中打开 ViewController 同时保持 Tabbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆