使用Swift在ScrollView中显示警报

时间:2016-08-17 09:04:46

标签: ios swift uiscrollview swift2 uialertview

我这里有这个问题。

我在我正在使用的库中找到了这个类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)

    }
}

错误就是这个。

  

类型的价值' ImageSlideshowItem'没有会员' presentViewController'

如何知道如何显示警报?

更新

这是我称之为ImageSlideshowItems

的另一个类的链接

"ImageSlideshow"

这是完整的ImageSlideShowItem类

"ImageSlideshowItems"

1 个答案:

答案 0 :(得分:1)

您遇到的问题是ScrollViewUIView对象的类型而不是controller,因此您没有presentViewControllerScrollView的子类中UIViewController 1}},为了解决你可以这样尝试的问题,在该类中创建一个scrollView实例,并在任何{{1时使用该实例初始化ViewController时使用该实例来呈现alertController ,只需传递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