简体   繁体   English

完成处理程序在viewDidLoad中不起作用?

[英]Completion Handler is not working in viewDidLoad?

I'm using this library in my app for banners. 我在我的应用程序中使用此来设置横幅广告。 I am trying to get the Link by parsing the JSON. 我试图通过解析JSON来获取链接。

The Images are are not showing in the slideshow view. slideshow视图中未显示图像。 If I press the slideshow view, after that everything works fine. 如果我按下slideshow视图,之后一切正常。 I thought that there was some issue with my completion handler. 我认为我的完成处理程序存在一些问题。 But I can't solve it yet :) 但我还是解决不了:)

@IBOutlet weak var slideshow: ImageSlideshow!
var transitionDelegate: ZoomAnimatedTransitioningDelegate?
var Banner : [AlamofireSource] = []

override func viewDidLoad() {
        super.viewDidLoad()
        Banners { (imagesource) in
            if imagesource != nil {
                self.bannershow()
            }
        }
  }

    func Banners(completionHandler: ([AlamofireSource]?) -> ()) -> (){
        Alamofire.request(.GET, "http://46.420.116.11/mobileapp/gps/api.php?rquest=get_banners")
            .responseJSON{ response in
                if let data = response.result.value{
                    let json = JSON(data)
                    let count = json["image_path"].count
                    for index in 0...count-1 {
                        let image :String = json["image_path"][index].stringValue
                        let source : AlamofireSource = AlamofireSource(urlString: image)!
                        self.Banner.append(source)
                    }
                    completionHandler(self.Banner)
                }
        }
  }

    func bannershow(){
        self.slideshow.backgroundColor = UIColor.whiteColor()
        self.slideshow.slideshowInterval = 2.0
        self.slideshow.contentScaleMode = UIViewContentMode.ScaleToFill
        self.slideshow.setImageInputs(self.Banner)

        let recognizer = UITapGestureRecognizer(target: self, action: "click")
        self.slideshow.addGestureRecognizer(recognizer)
        }

 func click() {
        let ctr = FullScreenSlideshowViewController()
        ctr.pageSelected = {(page: Int) in
            self.slideshow.setScrollViewPage(page, animated: false)
        }

        ctr.initialPage = slideshow.scrollViewPage
        ctr.inputs = slideshow.images
        self.transitionDelegate = ZoomAnimatedTransitioningDelegate(slideshowView: slideshow);
        ctr.transitioningDelegate = self.transitionDelegate!
        self.presentViewController(ctr, animated: true, completion: nil)
    }

You probably have a threading problem. 您可能遇到了线程问题。 There is no guarantee that the Banners completion handler is called on the main thread. 无法保证在主线程上调用Banners完成处理程序。 You need to step out to the main thread explicitly before doing anything that touches your properties or (especially) the interface. 在执行任何触及属性或(特别是)接口的操作之前,您需要明确地跳到主线程。

I think your problem might be that you're expecting the images to be available immediately but they need to be downloaded before, so they won't be available immediately after your viewDidLoad method finished. 我认为您的问题可能是您希望图像立即可用,但之前需要先下载,因此在viewDidLoad方法完成后它们将无法立即使用。 That's why you should probably configure your slideshow in the viewDidLoad and not in your bannershow() method. 这就是为什么您应该在viewDidLoad配置幻灯片,而不是在bannershow()方法中。 Something like this might be an improvement: 这样的事情可能会有所改善:

@IBOutlet weak var slideshow: ImageSlideshow!
var bannerImages : [AlamofireSource] = []

override func viewDidLoad() {
    super.viewDidLoad()
    slideshow.backgroundColor = UIColor.whiteColor()
    slideshow.slideshowInterval = 2.0
    slideshow.contentScaleMode = UIViewContentMode.ScaleToFill
    let recognizer = UITapGestureRecognizer(target: self, action: "click")
    slideshow.addGestureRecognizer(recognizer)

    getBanners { imagesource in
        self.showBanner()
    }
}

func getBanners(completionHandler: ([AlamofireSource]?) -> ()) -> (){
    Alamofire.request(.GET, "http://46.420.116.11/mobileapp/gps/api.php?rquest=get_banners")
        .responseJSON{ response in
            if let data = response.result.value{
                let json = JSON(data)
                let count = json["image_path"].count
                for index in 0...count-1 {
                    let image :String = json["image_path"][index].stringValue
                    let source : AlamofireSource = AlamofireSource(urlString: image)!
                    self.bannerImages.append(source)
                }

            }
            completionHandler(self.bannerImages)
    }
}

func showBanner() {
    slideshow.setImageInputs(bannerImages)
}

Move the code to viewWillAppear . 将代码移动到viewWillAppear

override func viewWillAppear(animated: Bool) {
    Banners { (imagesource) in
        if imagesource != nil {
            self.bannershow()
        }
    }
}
func Banners(completionHandler: ([AlamofireSource]?) -> ()) -> (){
    Alamofire.request(.GET, "http://46.420.116.11/mobileapp/gps/api.php?rquest=get_banners")
        .responseJSON{ response in
            if let data = response.result.value{
                let json = JSON(data)
                let count = json["image_path"].count
                for index in 0...count-1 {
                    let image :String = json["image_path"][index].stringValue
                    let source : AlamofireSource = AlamofireSource(urlString: image)!
                    self.Banner.append(source)
                }
                completionHandler(self.Banner)
            }
    }

} }

You are executing for loop in Banners fun 你正在执行Banners fun循环
for index in 0...count-1 { let image :String = json["image_path"][index].stringValue let source : AlamofireSource = AlamofireSource(urlString: image)! for index in 0 ... count-1 {let image:String = json [“image_path”] [index] .stringValue let source:AlamofireSource = AlamofireSource(urlString:image)! self.Banner.append(source) } Replace this code in some other method and place an Optional self.Banner.append(source)}在其他方法中替换此代码并放置一个Optional
var image :String? var image:String? = json["image_path"][index].stringValue = json [“image_path”] [index] .stringValue

or place an thumb image, That will make you confirm that image is downloaded successfully or not . 或放置拇指图像,这将使您确认图像是否成功下载。

Let me know if it works 如果有效,请告诉我

Thanks, Happy Coding 谢谢,快乐编码

Maybe you don't see images because you update them in a secondary thread, you have to perform image update in main thread; 也许你没有看到图像,因为你在辅助线程中更新它们,你必须在主线程中执行图像更新; In swift ( performSelectorOnMainThread() is not available), you may use something like this : 在swift(performSelectorOnMainThread()不可用)中,您可以使用以下内容:

 dispatch_async(dispatch_get_main_queue(), {

                    myslideshow.updateimage();

 })

Not really sure, but I am guessing that since your images need to get downloaded, they are nil when you call self.slideshow.setImageInputs(self.Banner) , which internally sets the image from the list to an imageview which is added inside the scrollView. 不太确定,但我猜测,因为你的图像需要下载,当你调用self.slideshow.setImageInputs(self.Banner)时它们是零,它们在内部将图像从列表设置为一个在视图中添加的图像self.slideshow.setImageInputs(self.Banner)滚动视图。 So one way I can think of is use SDWebImage to set the image to the imageView so that it updates the respective imageViews once the image is ready(downloaded). 所以我能想到的一种方法是使用SDWebImage将图像设置为imageView ,以便在图像准备好(下载)后更新相应的imageViews I think you will need to use it in the InputSource.swift class in the setToImageView(_:) method. 我想你需要在setToImageView(_:)方法的InputSource.swift类中使用它。 You will have to check this though, this is the only possible problem i could think of that might be causing your issue. 你必须检查这个,这是我能想到的唯一可能导致你的问题的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM