2016-11-09 39 views
0

我想知道如何检查当前显示的视图控制器。Swift ios检查哪个视图控制器存在

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

而如果是任何帮助我的应用程序是建立这样的:

(根)的TabBar

(TAB1) - >导航器 - > VC - > VC ...

( TAB2) - >导航控制器 - > VC - > VC ...

(TAB3) - >导航控制器 - > VC - > VC ...

(TAB4) - >导航控制器 - > VC - > vc ...

+0

没有我的回答解决问题了吗? – emresancaktar

回答

0

这个功能应该解决您的问题,只要发送指数号作为参数(forexample:0 TAB1,1 TAB2等)它您导航selectedIndexViewController

// 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 

Presented ViewController;

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

enter image description here

相关问题