【问题标题】:Swift ios check which viewcontroller is presentSwift ios 检查存在哪个视图控制器
【发布时间】:2016-11-09 17:07:32
【问题描述】:

我想知道如何检查当前显示的视图控制器。

当用户收到通知并按下它时,我想将用户带到某个 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

中,我不想运行此代码

另外,如果用户已经在 tab 0 中但在与 EventVc 不同的 VC 中,我可以运行上面的代码吗?

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

(根)标签栏

(tab1) -> 导航控制器 -> vc -> vc...

(tab2) -> 导航控制器 -> vc -> vc...

(tab3) -> 导航控制器 -> vc -> vc...

(tab4) -> 导航控制器 -> vc -> vc...

【问题讨论】:

  • 我的回答解决了你的问题吗?

标签: ios swift uiviewcontroller uinavigationcontroller


【解决方案1】:

这个函数应该可以解决你的问题,只需发送 index 数字作为参数(例如:tab1 为 0,tab2 为 1 等) 导航你@987654323 @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)
    }
}

你可以像这样找到Presenting ViewController

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

呈现的 ViewController;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    相关资源
    最近更新 更多