I am working on an iOS application which interacts with an API using alamofire and swiftyJSON. In this api I get a bunch of values returned and one of them is images. the Images returned comes in form of an array but i am able to iterate through the array and display a single image. To make the application more advanced, I decided to implement UIPageViewController class in my application. I called the API function and tried to pass my images into view controllers.
List of things in my storyboard
from Image passed from an API not displaying in a pageview container swift
List of things in my storyboard
- A UIPageView
- A UiViewController with a UIImage embeded into it.
protocol ProductImagesPageViewControllerDelegate: class {
func setupPageController(numberOfPages: Int)
func turnPageController(to index: Int)
}
class ProductPageVC: UIPageViewController {
weak var pageViewControllerDelegate: ProductImagesPageViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
automaticallyAdjustsScrollViewInsets = false
dataSource = self
delegate = self
}
func configureImg() {
let productId = ProductServices.instance.selectedProduct?.id
ProductServices.instance.findIndividualProducts(id: productId!, completion: { (success) in
if success {
ProductServices.instance.productDetails.forEach({ (productDetail) in
let images = productDetail.productImg.count
if images > 0 {
for image in 0..<(images) {
let imageVC = self.storyboard?.instantiateViewController(withIdentifier: PRODUCT_IMAGE_VC)
self.controllers.append(imageVC!)
}
}
})
}
})
}
lazy var controllers: [UIViewController] = {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var controllers = [UIViewController]()
let productId = ProductServices.instance.selectedProduct?.id
ProductServices.instance.findIndividualProducts(id: productId!, completion: { (success) in
if success {
ProductServices.instance.productDetails.forEach({ (productDetail) in
let images = productDetail.productImg.count
if images > 0 {
for image in 0..<(images) {
let imageVC = storyboard.instantiateViewController(withIdentifier: PRODUCT_IMAGE_VC)
controllers.append(imageVC)
}
}
})
}
})
self.pageViewControllerDelegate?.setupPageController(numberOfPages: controllers.count)
return controllers
}()
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.configureDisplaying(viewController: controller)
setViewControllers([controller], direction: direction, animated: true, completion: nil)
}
func configureDisplaying(viewController: UIViewController)
{
for (index, vc) in controllers.enumerated() {
if viewController === vc {
if let imageVC = viewController as? ImagesVC {
imageVC.configureImage(productIndex: index)
self.pageViewControllerDelegate?.turnPageController(to: index)
}
}
}
}
}
// MARK: - UIPageViewControllerDataSource
extension ProductPageVC : UIPageViewControllerDataSource
{
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController?
{
if let index = controllers.index(of: viewController) {
if index > 0 {
return controllers[index-1]
}
}
return controllers.last
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let index = controllers.index(of: viewController) {
if index < controllers.count - 1 {
return controllers[index + 1]
}
}
return controllers.first
}
}
extension ProductPageVC : UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController,
willTransitionTo pendingViewControllers: [UIViewController])
{
self.configureDisplaying(viewController: pendingViewControllers.first as! ImagesVC)
}
func pageViewController(_ pageViewController: UIPageViewController,
didFinishAnimating finished: Bool, previousViewControllers: [UIViewController],
transitionCompleted completed: Bool)
{
if !completed {
self.configureDisplaying(viewController: previousViewControllers.first as!
ImagesVC)
}
}
}
This is my ImageVC codeclass ImagesVC: UIViewController {
@IBOutlet weak var imageView: UIImageView!
func configureImage(productIndex : Int) -> Void {
ProductServices.instance.productDetails.forEach { (productImg) in
if productImg.productImg.count > 0 {
imageView.sd_setImage(with: URL(string: productImg.productImg[productIndex]!) )
} else {
imageView.image = UIImage(named: "image_not_found")
}
}
}}
This is my HeaderImageView codeclass HeaderImageVC: UIView {
@IBOutlet weak var pageControl: UIPageControl!
}
extension HeaderImageVC : ProductImagesPageViewControllerDelegate {
func setupPageController(numberOfPages: Int)
{
pageControl.numberOfPages = numberOfPages
}
func turnPageController(to index: Int)
{
pageControl.currentPage = index
}
}
I have no idea why the code is not working buth further codes would be supplied on request. Thanks.from Image passed from an API not displaying in a pageview container swift
https://www.wizweb.in
ReplyDeleteWizweb Technology is a leading software development company custom website design, software development, SMS Provider, Bulk sms, transactional sms, promotional sms, mobile app development, Hosting Solution, seo(search engine optimization) and Digital marketing etc.