如何显示特定的 VC ,更像是导航到 VC 的堆栈

How to show a specific VC , more like navigate to a stack of VC

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

UIApplication.shared.open(aUrl!)

我也接到了电话:

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

这是一个警报。

这个问题就像标题说的那样..我如何导航到特定的 VC ,(从 FirstApp 到我用通用 link 打开它的第二个),更像是映射一个 navigation controller stack

我写过类似的东西:

        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)
        }
    }

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

另外,如果我想用通用 link 处理更多,我可以为此制作一个助手 class 吗?任何建议表示赞赏。

好的,所以这不是一个普遍的 Link 问题,而是一个导航偏好。当您在 continueUserActivity 函数中传递通用 link 时,您应该解析它并从您的应用程序委托中相应地导航。你真的应该将你的通用 Link 处理切换到 Branch 因为他们会免费为你处理这个并且他们通过回调而不是 link 参数传递元数据,这将使你的 link 更强大。无论如何...

从 UINavigation 开始ViewController

这包括有一个 rootViewController,它应该是您主页的一个实例。将这个导航视图控制器存储为一个实例变量,这样你的应用程序在启动时只处理一个导航视图控制器。如果您使用的是故事板,那么您只需获取对该导航视图控制器的引用即可:

self.nav = self.window?.rootViewController as? UINavigationViewController

将 View Controller 推送到 Nav

当您收到 link 并确定需要将该用户带到新页面时,您应该决定是推送还是显示 VC。

推动

如果您想将该视图维护为堆栈的一部分,则需要推送。例如,如果您想向用户展示一双鞋子,您可能希望将它们放入鞋子类别中,然后使用鞋子详细信息控制器呈现它们。在这种情况下,您可以将用户带到主页并将适当的导航堆栈推送到您的 UINavigationViewController.

self.nav.popToRootViewController(animated: true)

self.nav.pushViewController(firstController, animated: true)

self.nav.pushViewController(secondViewController, animated: true)

这样你的用户 UINavigationController 堆栈将看起来像 root > firstVC > secondVC 他们将有面包屑可以遍历通过流量返回。

呈现

在您不想将视图压入堆栈的情况下。也许您想显示一个弹出窗口,而不管它们在应用程序中的什么位置,并且您不想弄乱它们的位置,您应该使用 present a VC。这将简单地在整个 NavigationController 上显示 ViewController,而不会添加到 NavigationController 的堆栈中。

self.nav.presentViewController(modalVC, animated:true, completion: nil)

您可以在 modalVC 中调用

self.dismiss(animated: true, completion: nil)

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

此 属性 描述和详细信息可以在 UINavigationController 文档中找到

viewControllers

Property

The view controllers currently on the navigation stack.

声明 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.

例子

    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)