ios - 如何使 3D Touch Quick Action 与 UITabBar 一起使用?

标签 ios swift uitabbarcontroller 3dtouch

我已经为此苦苦挣扎了几个小时。

我有一个 UITabBarController,里面有 UINavigationController,里面有一个 ViewController,里面有一个 tableview。

所有教程都以 NavigationController 作为父级,所有内容都在其中。

这是我的 Main.storyboard 的样子(我试图让快捷方式转到顶行的第三个 ViewController,或第二行向下的第二个 ViewController):

enter image description here


这是我的 info.plist 文件的一部分:

    <key>UIApplicationShortcutItems</key>
    <array>
        <dict>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeHome</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>Home</string>
            <key>UIApplicationShortcutItemType</key>
            <string>com.emmetarries.Tabbed-Test.home</string>
        </dict>
        <dict>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeLatest</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>Latest Photo</string>
            <key>UIApplicationShortcutItemType</key>
            <string>com.emmetarries.Tabbed-Test.latestPhoto</string>
        </dict>
    </array>

我已经研究过这些:

DuckDuckGoed:“使用 UITabBarController 进行 3D 快速操作”,除以下内容外没有结果。

Handle 3D Touch quick actions with UITabBarController and UINavigationController : objective-C

iOS 9 selecting tabbar index with quick actions, performActionForShortcutItem : objective-C

我已尝试按照我可以在 Stack Overflow 和 Youtube 中找到的所有教程进行操作。


有谁知道这是如何完成的,或者您是否找到了在 Swift 3 中解释这一点的教程?我可以重新排序/安排我的观点,我只是还没有找到一个好的方法。

最佳答案

试试这个:

在你的 AppDelegate.swift 中:

enum QuickAction: String {
case Home = "home"
case LatestPhoto = "latestPhoto"

init?(fullIdentifier: String) {
    guard let shortcutIdentifier = fullIdentifier.components(separatedBy: ".").last else {
        return nil
    }

    self.init(rawValue: shortcutIdentifier)
 }
}

我们将使用它来确定您选择的操作。然后我们需要实现处理 Action 的函数:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    completionHandler(handleQuickAction(shortcutItem: shortcutItem))
}

private func handleQuickAction(shortcutItem: UIApplicationShortcutItem) -> Bool {
    let shortcutType = shortcutItem.type
    guard let shortcutIdentifier = QuickAction(fullIdentifier: shortcutType)
        else {
            return false
    }

    guard let tabBarController = window?.rootViewController as? UITabBarController else {
      return false
    }

    switch shortcutIdentifier {
    case .Home:
        tabBarController.selectedIndex = 0
    case .LatestPhoto:
        tabBarController.selectedIndex = 1
    }
    return true
}

完成了!

关于ios - 如何使 3D Touch Quick Action 与 UITabBar 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43303642/

相关文章:

iphone - 如何有条件地不返回单元格

swift - 快速将字符串转换为谷歌可搜索字符串

ios - 在 swift 3 中做语言的更好方法

ios - 以编程方式创建 UITabBarController,仅显示一个选项卡

ios - 我在将此字符串传递给 View Controller segue (ios) 中的标签时做错了什么?

ios - UIPopoverController 显示的 UIViewController 的大小类错误

swift - 解析和 Swift : Column with multiple keys

ios - 在选项卡栏 Controller 中隐藏当前 View Controller 的选项卡

iOS:编辑选项卡位置后导航到特定选项卡

ios - iOS 中 ArrayList 的替代品?