1

我正在使用 atabViewController并想知道如何从主屏幕上进行 3D 触摸操作,从而调出一个选项卡。我已经有了代码,但似乎无法显示特定的选项卡;我只能得到一个窗口。我的 AppDelagate.swift 在下面。有什么帮助吗?

在此处输入图像描述

enum ShortcutIdentifier: String
{
    case First
    case Second

    init?(fullType: String)
    {
        guard let last = fullType.componentsSeparatedByString(".").last else {return nil}
        self.init(rawValue: last)
    }

    var type: String
        {
        return NSBundle.mainBundle().bundleIdentifier! + ".\(self.rawValue)"
    }

}

func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) -> Bool
{
    var handled = false

    guard ShortcutIdentifier(fullType: shortcutItem.type) != nil else {return false}
    guard let shortcutType = shortcutItem.type as String? else {return false}

    switch (shortcutType)
    {
    case ShortcutIdentifier.First.type:
        handled = true

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let navVC = storyboard.instantiateViewControllerWithIdentifier("sourcesview") as! UINavigationController
        if let tabBarController = navVC.topViewController as? UITabBarController {
            tabBarController.selectedIndex = 4
        }
        self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)

        break
    case ShortcutIdentifier.Second.type:
        handled = true

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let navVC = storyboard.instantiateViewControllerWithIdentifier("searchview") as! UINavigationController
        self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)

        break
    default:
        break
    }


    return handled

}
4

1 回答 1

0

您必须selectedTabUITabBarController. 我假设UINavigationController您从 nib 加载的包含一个UITabBarController顶视图控制器,因此在从 nib 加载导航控制器后,您必须访问选项卡栏控制器并将选定的选项卡属性设置为所需的选项卡。

switch (shortcutType)
    {
    case ShortcutIdentifier.First.type:
        handled = true

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let navVC = storyboard.instantiateViewControllerWithIdentifier("sourcesview") as! UINavigationController
        if let tabBarController = navVC.topViewController as? UITabBarController {
            tabBarController.selectedIndex = 1
        }
        self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)

        break
        ...
}
于 2015-10-30T15:25:20.600 回答