ios - 3d touch快速操作调用UIButton

标签 ios swift uibutton 3dtouch quickaction

我创建了一个在初始 VC 上有 4 个 UIButtons 的应用程序。 每个按钮都会转到另一个 VC(在所有情况下都相同),但参数不同。

 @IBAction func googleBtnPressed(_ sender: AnyObject) {
    searchEngine = "Google"
    print(searchEngine)
    performSegue(withIdentifier: "google", sender: nil)

}

@IBAction func bingBtnPressed(_ sender: AnyObject) {
    searchEngine = "Bing"
    print(searchEngine)
    performSegue(withIdentifier: "google", sender: nil)

}

@IBAction func ddgBtnPressed(_ sender: AnyObject) {
    searchEngine = "DuckDuckGo"
    print(searchEngine)
    performSegue(withIdentifier: "google", sender: nil)

}

@IBAction func customBtnPressed(_ sender: AnyObject) {
    performSegue(withIdentifier: "custom", sender: nil)

}

if let vc = segue.destination as? ViewController {
            if searchEngine == "Google" {
                    vc.url = NSURL(string: "https://www.google.com")
                    vc.segueUsed = "google"
                    vc.searchEngine = "google"
            }

            else if searchEngine == "Bing" {
                    vc.url = NSURL(string: "https://www.bing.com")
                    vc.segueUsed = "google"
                    vc.searchEngine = "bing"
            }

            else if searchEngine == "DuckDuckGo" {
                    vc.url = NSURL(string: "https://www.duckduckgo.com")
                    vc.segueUsed = "google"
                    vc.searchEngine = "duckduckgo"
            }
        }

    }else if segue.identifier == "custom"{
        print(segue.identifier!)
        if let vc = segue.destination as? ViewController {
            vc.segueUsed = "custom"
        }
    }

我想使用 3d Touch 的 Quick Actions 从主屏幕调用这 4 个按钮。 我已经在 info.plist 文件中创建了条目,但在处理 AppDelegate.swift

中的响应时遇到困难
<key>UIApplicationShortcutItems</key>
<array>
    <dict>
        <key>UIApplicationShortcutItemType</key>
        <string>$(PRODUCT_BUNDLE_IDENTIFIER).Google</string>
        <key>UIApplicationShortcutItemTitle</key>
        <string>Google</string>
        <key>UIApplicationShortcutItemIconType</key>
        <string>google</string>
    </dict>
</array>

我该如何实现?

感谢您的帮助。

最佳答案

let yourViewcontroller = self.window?.rootViewController// This view controller should be action view controller
        let itemKey = shortcutItem.userInfo["<YOUR KEY>"]
        if itemKey == "Google" {
            //call google action method
        } else if itemKey == "Bing" {
            //call Bing action method
        } else if itemKey == "Ddg" {
            //call "Ddg" action method
        }else if itemKey == "CustomButton" {
            //call CustomButton action method
        }

注意:您可以将itemKey保存在ShortCutItemDictionary中。

关于ios - 3d touch快速操作调用UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41283658/

相关文章:

iphone - 如何使用 Xcode 4 创建简单的 iOS 应用程序原型(prototype)?

ios - Xcode 项目为 FIRAnalyticsConnector.framework 提供 pod 文件错误。如何解决?

iOS 7 - 如何拖动按钮或 UIView 的快照而不是按钮本身

ios - UIButton 上的 UILongPressGestureRecognizer 不起作用

iphone - 使用社交框架将图像添加到 Facebook

ios - 异步请求/响应 : When am I really done?

ios - 为设备构建时遇到错误 - flutter

ios - 主线程上的异步任务与同步任务相同吗?

ios - 将结构作为核心数据实体的属性

iphone - Objective-C 使用 typedef enum 来设置类行为,就像 Cocoa 一样