gpt4 book ai didi

ios - 使用 Swift 在 ScrollView 中显示警报

转载 作者:行者123 更新时间:2023-11-28 16:16:53 27 4
gpt4 key购买 nike

我这里有这个问题。

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

知道如何显示警报吗?

更新:

这是我称之为 ImageSlideshowItems 的另一个类的链接

"ImageSlideshow"

这是完整的 ImageSlideShowItem 类

"ImageSlideshowItems"

最佳答案

您面临的问题是 ScrollViewUIView 对象的类型,而不是 controller,因此您无法获得 presentViewControllerScrollView 的子类中,要解决这个问题,你可以这样尝试,在该类中创建一个 UIViewController 实例,并使用该实例来呈现alertController,当你在任何 ViewController 上初始化 scrollView 时,只需像这样传递那个 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)
}
}

}

现在将当前 ViewController 的实例与您的 scrollView 对象一起传递。

let imgslider = ImageSlideshowItem() 
imgslider.viewController = self

关于ios - 使用 Swift 在 ScrollView 中显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38992316/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com