迁移到Swift 2后出现UIActivityViewController错误

时间:2015-10-04 06:24:26

标签: ios swift2 uiactivityviewcontroller

ShareButton函数在Xcode 6中有效,但在升级到行中的Xcode 7后出错:

  

avc.completionWithItemsHandler = {

我的代码:

 @IBAction func ShareButton(sender: AnyObject) {

    let things = ["Thank you"]

    let avc = UIActivityViewController(activityItems:things, applicationActivities:nil)

    avc.setValue("Salon Job HK", forKey: "subject")

    avc.completionWithItemsHandler = {

        (s: String!, ok: Bool, items: [AnyObject]!, err:NSError!) -> Void in

    }

    self.presentViewController(avc, animated:true, completion:nil)

    if let pop = avc.popoverPresentationController {

        let v = sender as! UIView // sender would be the button view tapped, but could be any view

        pop.sourceView = v

        pop.sourceRect = v.bounds

    }

}

3 个答案:

答案 0 :(得分:9)

只需检查Swift 2.0 UIActivityViewControllerCompletionWithItemsHandler的声明:

  

typealias UIActivityViewControllerCompletionWithItemsHandler =   (String?,Bool,[AnyObject] ?, NSError?) - >空隙

尝试这样:

avc.completionWithItemsHandler = {

    (s: String?, ok: Bool, items: [AnyObject]?, err:NSError?) -> Void in

}

或简单地说:

avc.completionWithItemsHandler = {
    (s, ok, items, err) in

} 

Swift 3

@IBAction func shareButton(sender: Any) {  
    let things = ["Thank you"]
    let avc = UIActivityViewController(activityItems: things, applicationActivities: nil)
    avc.setValue("Salon Job HK", forKey: "subject")
    avc.completionWithItemsHandler = {
        (activityType, completed, returnedItems, activityError) in
        // code
    }
    present(avc, animated: true)
}

答案 1 :(得分:6)

对于 Swift 3.0

,这又略有改变
UIActivityViewControllerCompletionWithItemsHandler = (UIActivityType?, Bool, [Any]?, Error?) -> Void

答案 2 :(得分:0)

除了上面的答案,您还可以将其设置为nil,因为在调用完成时您似乎没有做任何事情

avc.completionWithItemsHandler = nil