ios - Swift ios 检查哪个 View Controller 存在

标签 ios swift uiviewcontroller uinavigationcontroller

我想知道如何检查当前显示的 View Controller 。

当用户收到通知并按下它时,我想将用户带到某个 VC。但前提是用户尚不存在。

当我收到通知时我正在使用它:

if let tabBarController = self.window!.rootViewController as? CustomTabBarController {
                        let navInTab:UINavigationController = tabBarController.viewControllers?[0] as! UINavigationController
                        let storyboard = UIStoryboard(name: "Main", bundle: nil)
                        let destinationViewController = storyboard.instantiateViewController(withIdentifier: "EventVc") as? EventsViewController
                        navInTab.pushViewController(destinationViewController!, animated: true)

                        tabBarController.selectedIndex = 0
                    }

如果用户已经在 EventVc 中,我不想运行此代码

此外,如果用户已位于选项卡 0 但位于与 EventVc 不同的 VC 中,我可以运行上面的代码吗?

如果有任何帮助的话,我的应用程序是这样构建的:

(根)标签栏

(tab1) -> 导航 Controller -> vc -> vc...

(tab2) -> 导航 Controller -> vc -> vc...

(tab3) -> 导航 Controller -> vc -> vc...

(tab4) -> 导航 Controller -> vc -> vc...

最佳答案

这个函数应该可以解决您的问题,只需发送索引数字作为参数(例如:0表示tab1,1表示tab2等),它就会导航您selectedIndex ViewController.

// Navigation TabIndex TabBar View Controller
fileprivate func goTabIndex(index: Int) {
    let storyboard = UIStoryboard(name: "yourTabbarStoryboardID", bundle: nil)
    let tabBarController = storyboard.instantiateViewController(withIdentifier: "EventVc") as! UITabBarController
    tabBarController.selectedIndex = index
    self.window?.makeKeyAndVisible()
    DispatchQueue.main.async {
        self.window?.rootViewController?.present(tabBarController, animated: false)
    }
}

你可以像这样找到呈现ViewController

let presentingVC = self.window?.rootViewController?.presentingViewController

呈现ViewController;

let presentedVC = self.window?.rootViewController?.presentedViewController

enter image description here

关于ios - Swift ios 检查哪个 View Controller 存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40512407/

相关文章:

swift - 字符串的模式匹配

ios - 将 DSP 过滤器应用于 iOS 中的数据

ios - Objective-C:UIViewController 中的 TableView - 单击后仅加载数据

ios - UIPageViewController 从子 UIViewController 上的控件过渡

ios - self.navigationController.tabBarController.selectedIndex 不工作

ios - 以编程方式更新 info.plist 中的值

ios - 如何将 IBOutlet 值从 View 传递给 VIPER iO 中的交互器?

ios - 在 Swift 中从 Firebase 读取多个子节点

iphone - iOS 等同于 Android 片段/布局

swift - 我用 github/fusuma opensource 拍摄的照片旋转了 90 度