如何显示特定的 VC,更像是导航到一堆 VC [英] How to show a specific VC , more like navigate to a stack of VC

查看:31
本文介绍了如何显示特定的 VC,更像是导航到一堆 VC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在我的应用程序中实现了通用链接,当按下按钮时,我打开了我的第二个应用程序:

<块引用>

UIApplication.shared.open(aUrl!)

我也接到了电话:

<块引用>

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restoreHandler: @escaping ([Any]?) -> Void) -> Bool {}

这是一个警报.

问题就像标题所说的..如何导航到特定的 VC ,(从 FirstApp 到我使用通用链接打开它的 Second),更像是映射导航控制器堆栈 .

我写过类似的东西:

 if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyAdsController") as?MyAdsViewController {如果让 window = self.window,让 rootViewController = window.rootViewController {var currentController = rootViewController而让presentedController = currentController.presentedViewController {currentController =presentedController}currentController.present(控制器,动画:真,完成:零)}}

但这是一个丑陋的表示,我需要像洞导航堆栈这样的东西......

此外,如果我想使用通用链接处理更多内容,我可以为此创建一个帮助类吗?任何建议表示赞赏.

解决方案

您需要使用 UINavigationController.viewControllers 属性,您可以使用 setViewControllers(_:animated:) 方法或直接修改 .viewControllers 属性,其中 rootViewController 将是 viewControllersArray[0]topViewController 将是 viewControllersArray[count-1]

这个属性的描述和细节可以在UINavigationController文档中找到

viewControllers

<块引用>

财产

当前在导航堆栈上的视图控制器.

声明SWIFT

var viewControllers: [UIViewController]

<块引用>

讨论 根视图控制器位于数组中的索引 0,后视图控制器位于索引 n-2,顶部控制器位于索引 n-1,其中 n 是数组中的项数.

为这个属性分配一个新的视图控制器数组是相当于调用 setViewControllers:animated: 方法动画参数设置为 false.

示例

 let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)让 firstViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController") 作为?第一视图控制器让 secondViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") 作为?第二个视图控制器让 thirdViewController = storyboard.instantiateViewController(withIdentifier: "ThirdViewController") 作为?第三个视图控制器self.navigationController?.setViewControllers([firstViewController,secondViewController,thirdViewController], 动画: false)

So i've implemented universal link in my app and when pressing on a button I open my second app with :

UIApplication.shared.open(aUrl!)

also I'm getting the call in :

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {}

which is an alert.

The question is like the title says..how could I navigate to a specific VC ,(from the FirstApp to the Second which i opened it with universal link), more like mapping a navigation controller stack .

I've writed something like :

        if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyAdsController") as? MyAdsViewController {
        if let window = self.window, let rootViewController = window.rootViewController {
            var currentController = rootViewController
            while let presentedController = currentController.presentedViewController {
                currentController = presentedController
            }
            currentController.present(controller, animated: true, completion: nil)
        }
    }

but it's an ugly representation, i need something like the hole navigation stack...

And also , can i make a helper class for this if i want to handle more with universal link? Any suggestion are appreciated.

解决方案

You need to use the .viewControllers property of your UINavigationController you can do this using setViewControllers(_:animated:) method or modifying directly .viewControllers property where rootViewController will be the viewControllersArray[0] and topViewController will be viewControllersArray[count-1]

This property description and details can be founded in the UINavigationController documentation

viewControllers

Property

The view controllers currently on the navigation stack.

Declaration SWIFT

var viewControllers: [UIViewController]

Discussion The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.

Assigning a new array of view controllers to this property is equivalent to calling the setViewControllers:animated: method with the animated parameter set to false.

example

    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let firstViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController") as? FirstViewController
    let secondViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
    let thirdViewController = storyboard.instantiateViewController(withIdentifier: "ThirdViewController") as? ThirdViewController

    self.navigationController?.setViewControllers([firstViewController,secondViewController,thirdViewController], animated: false)

这篇关于如何显示特定的 VC,更像是导航到一堆 VC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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