3
我嘗試使用Alamofire執行GET請求時出現問題。在我的viewDidLoad方法中。我已經檢查過我的請求正常工作,我的數組已經填充,但是當我運行代碼時,我得到一個致命錯誤:索引超出範圍,因爲當我調用getViewControllerAtIndex方法時,數組是空的。 如果我想使用來自我的API的數據創建我的UIPageViewController,該怎麼辦? 這是我在ViewController.swift在API請求後更新UIPageViewController的正確方法
import UIKit
import Foundation
import Alamofire
class ViewController: UIPageViewController, UIPageViewControllerDataSource {
var arrPageTitle = [String]()
var arrPagePhoto = [String]()
override func viewDidLoad() {
super.viewDidLoad()
let guidesEndpoint: String = "http://testapi.eu-gb.mybluemix.net/guides"
Alamofire.request(guidesEndpoint).responseJSON { (response) in
if let JSON = response.result.value {
//print("JSON: \(JSON)")
if let guides = JSON as? [String : AnyObject] {
if let guideResourceList = guides["_embedded"] as? [String : AnyObject] {
//print(guideResourceList)
if let guidesArray = guideResourceList["guideResourceList"] as? [AnyObject] {
for guide in guidesArray {
if let guideGuide = guide["guide"] as? [String : AnyObject] {
if let textItem = guideGuide["text"] as? String {
// Populate arrPageTitle
self.arrPageTitle.append(textItem)
print(textItem)
}
}
if let guideLinks = guide["_links"] as? [String : AnyObject] {
if let linksObj = guideLinks["imageUrl"] as? [String : String] {
if let link = linksObj["href"] {
// Populate arrPagePhoto
self.arrPagePhoto.append(link)
print(link)
}
}
}
}
}
}
}
}
}
self.dataSource = self
self.setViewControllers([getViewControllerAtIndex(0)] as [UIViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
// Use this method to return to previous view. In this I have put a condition such that if it is first view then return nil otherwise return ViewController.
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let pageContent: PageContentViewController = viewController as! PageContentViewController
var index = pageContent.pageIndex
if index == 0 || index == NSNotFound {
return nil
}
index -= 1
return getViewControllerAtIndex(index)
}
// MARK:- UIPageViewControllerDataSource Methods
// Use this method to return next view. In this I have put a condition such that if it is last view then return nil otherwise return ViewController.
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let pageContent: PageContentViewController = viewController as! PageContentViewController
var index = pageContent.pageIndex
if index == NSNotFound { return nil }
index += 1
if index == arrPageTitle.count { return nil }
return getViewControllerAtIndex(index)
}
// MARK:- Custom Methods
func getViewControllerAtIndex(_ index: Int) -> PageContentViewController {
// Create a new view controller and pass suitable data.
let pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageContentViewController") as! PageContentViewController
// fatal error: Index out of range
pageContentViewController.strTitle = "\(arrPageTitle[index])"
pageContentViewController.strPhotoName = "\(arrPagePhoto[index])"
pageContentViewController.pageIndex = index
pageContentViewController.numberOfPages = arrPagePhoto.count
print("\(arrPagePhoto.count)")
return pageContentViewController
}
}
代碼,這是我PageContentViewController.swift
import UIKit
class PageContentViewController: UIViewController {
@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var pageControl: UIPageControl!
var pageIndex: Int = 0
var strTitle: String!
var strPhotoName: String!
var numberOfPages: Int!
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = UIImage(named: strPhotoName)
lblTitle.text = strTitle
pageControl.currentPage = pageIndex
pageControl.numberOfPages = numberOfPages
}
}
任何想法?
哦完美,現在工作正常。非常感謝@Moriya –