使用 Swift 在 ScrollView 中显示警报

Display Alert in ScrollView with Swift

我这里有这个问题。

我在我正在使用的库中得到了这个 class public class ImageSlideshowItem: UIScrollView, UIScrollViewDelegate {。 所以我想在 scrollView 中添加一个 alertController 但我在使用此代码时遇到错误

func saveImage() {

     UIImageWriteToSavedPhotosAlbum(self.imageView.image!, self, "image:didFinishSavingWithError:contextInfo:", nil)
}

func image(image: UIImage!, didFinishSavingWithError error: NSError!, contextInfo: AnyObject!) {
    if (error != nil) {
        // Something wrong happened.
        print("not saved")
    } else {
        // Everything is alright.
        let alert = UIAlertController(title: "My Title", message: "This is my message.", preferredStyle: UIAlertControllerStyle.Alert)

        // add an action (button)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        var fullscree: FullScreenSlideshowViewController = FullScreenSlideshowViewController()
        // show the alert
        self.presentViewController(alert, animated: true, completion: nil)

    }
}

而这个错误。

Value of type 'ImageSlideshowItem' has no member 'presentViewController'

知道如何显示警报吗?

更新:

这是另一个 class 的 link,我称之为 ImageSlideshowItems

"ImageSlideshow"

这是完整的 ImageSlideShowItem class

"ImageSlideshowItems"

您面临的问题是 ScrollViewUIView 对象的类型而不是 controller,因此您不会在该子对象中得到 presentViewController [= ScrollView 的 26=],要解决这个问题,您可以这样尝试,在 class 中创建一个 UIViewController 实例,并使用该实例来呈现 alertController,当您初始化 scrollView,在任何 ViewController 上,只需像这样

传递那个 viewController 的引用
public class ImageSlideshowItem: UIScrollView, UIScrollViewDelegate {

    var viewController: UIViewController?

    func image(image: UIImage!, didFinishSavingWithError error: NSError!, contextInfo: AnyObject!) {
        if (error != nil) {
            // Something wrong happened.
            print("not saved")
        } else {
            // Everything is alright.
            let alert = UIAlertController(title: "My Title", message: "This is my message.", preferredStyle: UIAlertControllerStyle.Alert)

            // add an action (button)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            var fullscree: FullScreenSlideshowViewController = FullScreenSlideshowViewController()
            // show the alert
            self.viewController?.presentViewController(alert, animated: true, completion: nil)              
        }
    }  

}

现在用您的 scrollView 对象传递当前 ViewController 的实例。

let imgslider = ImageSlideshowItem() 
imgslider.viewController = self