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

时间:2016-02-13 18:16:08

标签: ios swift viewdidload completionhandler

我在我的应用中使用此library横幅广告。我试图通过解析JSON来获取链接。

slideshow视图中未显示图像。如果我按下@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) } 视图,之后一切正常。我认为我的完成处理程序存在一些问题。 但我还无法解决它:)

{{1}}

6 个答案:

答案 0 :(得分:4)

您可能遇到线程问题。无法保证在主线程上调用Banners完成处理程序。在执行任何触及属性或(特别是)接口的事情之前,您需要明确地跳到主线程。

答案 1 :(得分:0)

我认为您的问题可能是您希望图像立即可用,但之前需要下载,因此在viewDidLoad方法完成后,它们不会立即可用。这就是为什么您应该在viewDidLoad而不是bannershow()方法中配置幻灯片的原因。这样的事情可能会有所改善:

@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)
}

答案 2 :(得分:0)

将代码移至 viewWillAppear

override func viewWillAppear(animated: Bool) {
    Banners { (imagesource) in
        if imagesource != nil {
            self.bannershow()
        }
    }
}

答案 3 :(得分:0)

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)
            }
    }

}

你正在横幅乐队执行循环乐趣     索引在0 ... count-1 {                         let image:String = json [“image_path”] [index] .stringValue                         让source:AlamofireSource = AlamofireSource(urlString:image)!                         self.Banner.append(源)                     }   用其他方法替换此代码并放置一个可选的
    var image:String? = json [“image_path”] [index] .stringValue

或放置拇指图像,这将使您确认图像是否已成功下载。

让我知道它是否有效

谢谢,快乐编码

答案 4 :(得分:0)

也许你看不到图像,因为你在辅助线程中更新它们,你必须在主线程中执行图像更新; 在swift(performSelectorOnMainThread()不可用)中,您可以使用以下内容:

 dispatch_async(dispatch_get_main_queue(), {

                    myslideshow.updateimage();

 })

答案 5 :(得分:-1)

不太确定,但我猜测,因为您的图像需要下载,所以当您调用self.slideshow.setImageInputs(self.Banner)时它们是零,内部将图像从列表中设置为在scrollView中添加的imageview。因此我可以想到的一种方法是使用SDWebImage将图像设置为imageView,以便在图像准备好(下载)后更新相应的imageViews。我认为您需要在InputSource.swift方法的setToImageView(_:)类中使用它。你必须检查这个,这是我能想到的唯一可能导致你的问题的问题。