是否可以在完成处理程序中包含一个函数?

时间:2016-08-31 23:19:49

标签: swift

是否可以在完成处理程序中有一个函数?
我希望在用户在活动控制器中共享同一图像后通过函数保存图像。 我想做以下事情:

let controller = UIActivityViewController(activityItems: [generateMemedImage()], applicationActivities: nil)
        self.presentViewController(controller, animated: true, completion: nil)
        controller.completionWithItemsHandler = { 
            // Have this save() function when the activity has been completed
            func save() {
                //Save the meme
                let meme = Meme(topText: self.upperTextField.text!, lowerText: self.lowerTextField.text!, originalImage:
                    self.imageRetrieved.image!, memedImage: generateMemedImage())
            }
        }

有没有更好的解决方案?或者我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您不需要在完成处理程序中声明一个函数。只需编写代码来执行其中的保存,看看

class Controller: UIViewController {

    func foo() {
        let controller = UIActivityViewController(activityItems: [generateMemedImage()], applicationActivities: nil)
        self.presentViewController(controller, animated: true, completion: nil)
        controller.completionWithItemsHandler = { activityType, completed, returnedItems, error -> () in
            // write code to perform the save here <---
        }
    }

    func generateMemedImage() -> [AnyObject] {
        return []
    }

}