【问题标题】:How to show a specific VC , more like navigate to a stack of VC如何显示特定的 VC,更像是导航到一堆 VC
【发布时间】:2018-08-09 14:03:16
【问题描述】:

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

UIApplication.shared.open(aUrl!)

我也接到了电话:

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

这是一个警报。

问题就像标题所说的那样..我如何导航到特定的 VC (从 FirstApp 到我用通用链接打开的 SecondApp),更像是映射 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)
        }
    }

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

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

【问题讨论】:

  • 您的意思是,您希望导航到从第一个应用程序打开的第二个应用程序中的特定视图控制器?
  • 是的,完全正确。我已经编辑了我的问题
  • 那可能是:medium.com/@stasost/…
  • 您是否有理由不使用 UINavigationController 并重置您想要的堆栈(navigationController.viewControllers = [vc1, vc2, controllerYouWant])?
  • 不,一点也不,问题是我不知道怎么做 :)

标签: ios swift swift3 uinavigationcontroller ios-universal-links


【解决方案1】:

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

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

viewControllers

房产

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

声明 SWIFT

var viewControllers: [UIViewController]

讨论 根视图控制器在数组中的索引为 0,后视图控制器在索引 n-2,顶部控制器在 索引 n-1,其中 n 是数组中的项目数。

为这个属性分配一个新的视图控制器数组是 相当于调用 setViewControllers:animated: 方法 动画参数设置为 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)

【讨论】:

    【解决方案2】:

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

    从 UINavigationViewController 开始

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

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

    在导航上推送与呈现视图控制器

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

    如果您想将该视图作为堆栈的一部分进行维护,则推送是必要的。例如,如果您想向用户展示一双鞋子,您可能希望将它们纳入鞋子类别,然后使用鞋子细节控制器呈现它们。在这种情况下,您可以将用户带到主页并将正确的导航堆栈推送到您的 UINavigationViewController。

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

    这样,您的用户 UINavigationController 堆栈将看起来像 root > firstVC > secondVC,并且他们将拥有面包屑以便能够遍历流程。

    介绍

    如果您不想将视图推送到堆栈上。也许您想展示一个弹出窗口,无论他们在应用程序中的哪个位置,并且您不想弄乱他们的位置,您应该使用展示 VC。这只会在整个 NavigationController 上显示一个 ViewController,而不会添加到 NavigationController 的堆栈中。

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

    您可以在modalVC 内拨打电话

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 2019-11-25
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多