1

を使用してtabViewControllerいますが、ホーム画面から 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

selectedTabでプロパティを設定する必要がありますUITabBarControllerUINavigationControllernib からロードした には as top ビュー コントローラーが含まれていると想定している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 に答える