Func没有正确执行(Swift 3)

时间:2017-04-14 09:49:06

标签: ios swift swift3

我还是iOS开发的新手。我的想法是想从images(数据模型)中添加banner值。所以我需要调用func getAllBanner来获取所有值。然后只调用func turnToPage

问题是let controller = controllers[index]

  

致命错误:索引超出范围

当我调试时,我注意到lazy var controllers: [UIViewController]。那是为什么?

非常感谢任何帮助。

var images: [UIImage]?

var banner: [Banner]?

 //  Variable that not create onDidLoad
    lazy var controllers: [UIViewController] = {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        var controllers = [UIViewController]()

        if let images = self.images {
            for image in images {
                let sliderImageVC = storyboard.instantiateViewController(withIdentifier: Storyboard.sliderImageViewController)

                controllers.append(sliderImageVC)
            }
        }
        self.pageViewControllerDelegate?.setupPageController(numberOfPages: controllers.count)

        return controllers
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        automaticallyAdjustsScrollViewInsets = false
        dataSource = self
        delegate = self

        self.getAllBanner(strAppID: "1", locationid: "2")
        self.turnToPage(index: 0)
    }


//  To allow to turn page
    func turnToPage(index: Int) {
        let controller = controllers[index]
        var direction = UIPageViewControllerNavigationDirection.forward

        if let currentVC = viewControllers?.first {
            let currentIndex = controllers.index(of: currentVC)

            if currentIndex! > index {
                direction = .reverse
            }
        }

        self.configureViewDisplaying(viewController: controller)

        setViewControllers([controller], direction: direction, animated: true, completion: nil)
    }


//  Get Banner
    func getAllBanner(strAppID: String, locationid: String) {
        Banner.getBanner(strAppID: strAppID, locationID: locationid) { [weak self] banner in
            guard let `self` = self else {
                return
            }
            self.banner = banner

            //  Update UI here
            if let banners = self.banner {
                for banner in banners {
                    let endPoint = URL(string: banner.ImageURL)
                    do {
                        let data = try Data(contentsOf: endPoint!)
                        self.images = [UIImage(data: data)!]
                    } catch {
                        print("Error")
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用此代码段。

func getAllBanner(strAppID: String, locationid: String) {
    Banner.getBanner(strAppID: strAppID, locationID: locationid) { [weak self] banner in
        guard let `self` = self else {
            return
        }

         self.banner = banner

        //  Update UI here
        if let banners = self.banner {
            var images: [UIImage] = []
            for banner in banners {
                let endPoint = URL(string: banner.ImageURL)
                do {
                    let data = try Data(contentsOf: endPoint!)
                    images.append(UIImage(data: data)!)
                } catch {
                    print("Error")
                }
            }
          self.images = images
          // call turnpage method here 
        }
    }
}