Swift ios将用户发送到基于选项卡的应用程序中的特定ViewController

时间:2016-11-09 13:52:05

标签: ios swift xcode viewcontroller

我想在点击通知后将用户发送到我的应用中的特定ViewController。

我现在可以这样做:

var container = new Container();
container.Sonos = new List<Dictionary<string, List<Cfg>>>
{
    new Dictionary<string, List<Cfg>>
    {
        { "192.168.10.214", new List<Cfg> { new Cfg { Volume = "5" } } }
    }
};

var json = JsonConvert.SerializeObject(container);

但我的应用有一个标签栏,看起来像这样

标签栏

tab1:navigationController - &gt; VC1

tab2:navigationController - &gt; VC2 - &gt; HomeVC

tab:navigationController - &gt; VC3

每个标签都有一个navigationController作为它的前面。

那么如何将用户发送到HomeVC?我必须首先选择选项卡2,然后选择导航控制器,然后按下用户设备:

tab2:navigationController - &gt; VC2 - &gt; HomeVC

另一个问题是,如果有任何方法可以判断用户是否已经在HomeVC中?如果他已经在那里,我不想将用户发送到同一个VC。

3 个答案:

答案 0 :(得分:3)

您必须有权访问UITabbarController UIApplicationDelegate或您处理通知点击的任何地方。

let tabBar:UITabBarController = self.window?.rootViewController as! UITabBarController //or whatever your way of getting reference is

首先,您将在第二个标签中获得UINavigationController的引用,如下所示:

let navInTab:UINavigationController = tabBar.viewControllers?[1] as! UINavigationController

现在在第二个标签的导航控制器中推送您的主视图:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("Home") as? HomeViewController
navInTab.pushViewController(destinationViewController!, animated: true)

最后将标签切换到第二个以显示刚推出的家庭控制器

tabBar.selectedIndex = 1

请注意,此答案假定您的应用程序在处理通知点击之前已将标签栏设置为应用程序窗口的根视图控制器。

答案 1 :(得分:0)

尝试这样的事情:

if let tabBarController = window?.rootViewController as? UITabBarController {
  tabBarController.selectedIndex = 1 // in your case the second tab
}

我们的想法是切换到获取标签栏实例并将其切换到所需的标签(您拥有视图控制器)。

以上代码适用于AppDelegate /您可以通过获取tabBarController实例轻松地在任何地方调用它。

答案 2 :(得分:0)

您可以使用方法var selectedIndex: Int检查用户选择的标签。您可以查看view controller之类的self.navigationController?.presentingViewController?.presentedViewController。这将解决您的问题。