2017-08-02 32 views
0

我試圖實施股權shortcutItem(我的實現):斯威夫特:實現shortcutItem(3D觸摸)股項的錯誤「類型的AppDelegate的價值已經沒有現在的成員」

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { 
    if shortcutItem.type == "share"{ 
     self.shareItem() 
    } 
} 
func shareItem() { 

    let visitedlink = "http://google.com" 
    let myWebsite = NSURL(string: visitedlink) 
    let img: UIImage = UIImage(named:"Logo")! 
    guard let url = myWebsite else { 
     print("nothing found") 
     return 
    } 
    let shareItems:Array = [img,url] 
    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil) 
    activityViewController.excludedActivityTypes = [UIActivityType.print, UIActivityType.postToWeibo, UIActivityType.copyToPasteboard, UIActivityType.addToReadingList, UIActivityType.postToVimeo, UIActivityType.message, UIActivityType.mail] 

    self.present(activityViewController, animated: true, completion: nil) 
} 

但在這行:

self.present(activityViewController, animated: true, completion: nil) 

我得到這個錯誤:

Value of type AppDelegate has no member of present 

enter image description here

你們誰都知道我爲什麼會遇到這個錯誤?或者解決這個問題?

我真的很感謝你的幫助。

+0

錯誤消息是相當清楚的。 AppDelegate類不是UIViewController的子類。它沒有'present'方法。 – vadian

+0

@vadian:如何在任何視圖控制器上實現? – user2924482

+0

您需要獲取對當前頂部視圖控制器的引用。方式取決於根視圖控制器的實現。 – vadian

回答

0

試試這個,

public extension UIViewController { 
    func shareItem() { 
     let visitedlink = "http://google.com" 
     let myWebsite = NSURL(string: visitedlink) 
     let img: UIImage = UIImage(named:"Logo")! 
     guard let url = myWebsite else { 
      print("nothing found") 
      return 
     } 
     let shareItems:Array = [img,url] 
     let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil) 
     activityViewController.excludedActivityTypes = [UIActivityType.print, UIActivityType.postToWeibo, UIActivityType.copyToPasteboard, UIActivityType.addToReadingList, UIActivityType.postToVimeo, UIActivityType.message, UIActivityType.mail] 

     self.present(activityViewController, animated: true, completion: nil) 
    } 
} 
相關問題