簡體   English   中英

3d觸摸快速動作調用UIButton

[英]3d touch quick action call UIButton

我創建了一個在初始VC上有4個UIButton的應用程序。 每個按鈕都會切換到另一個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的快速操作從主屏幕調用這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中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM