2015-12-11 31 views
2

我有一个自定义单元格。它有一个UIImageView和一个按钮。当我点击按钮时,我的应用会将我的图像保存到相册并显示结果提醒。用自定义单元格快速显示警报

这里我的代码:

@IBAction func saveImage(sender: UIButton) { 
     UIImageWriteToSavedPhotosAlbum(self.photo.image!, self, "saveImageToLibrary:didFinishSavingWithError:contextInfo:", nil) 
    } 

func saveImageToLibrary(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) { 
    let vc = ViewController() 
    if error == nil { 
     let ac = UIAlertController(title: "Saved!", message: "Image has been saved to your photos.", preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     vc.presentViewController(ac, animated: true, completion: nil) 
    } else { 
     let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     vc.presentViewController(ac, animated: true, completion: nil) 
    } 
} 

但是,当我点击的tableview我保存按钮,将其与气候变暖成功保存图像:

Warning: Attempt to present <UIAlertController: 0x7fa452f74ee0> on <Test.ViewController: 0x7fa452f56f40> whose view is not in the window hierarchy! 

我如何可以调用从自定义单元格的警告?

回答

1

我想这个使用委托解决: 在您的自定义单元格添加此代码

var delegate: UIViewController? 

,并在您的tableView(的tableView:的cellForRowAtIndexPath)加入这一行:

cell.delegate = self 

终于在您的saveImageToLibrary实现将vc替换为delegate?。这个?是因为您创建了委托作为可选。

+0

谢谢。我尝试并取得成功。 – Ashley

+0

你可能想让委托对象成为一个弱变量以避免数据泄露 –

相关问题