Swift 4 UIPageViewController 未显示正确的视图(按顺序在数组中定义) [英] Swift 4 UIPageViewController not showing the correct views (as defined in an array in sequence

查看:26
本文介绍了Swift 4 UIPageViewController 未显示正确的视图(按顺序在数组中定义)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个小型报价应用程序.引号位于对象数组中.有些人有一篇文章,其中提供了有关报价的更多信息.

I am trying to build a small quotes app. The quotes are in an object array. Some have an article which provides some more info about the quote.

我正在使用 2 种视图控制器类型.一个有文章的文本块,另一个没有.

I am using 2 viewcontroller types. One has a text block for the article and the other one doesn't.

UIPageViewController 类被设置为允许用户滑动引号.这是由上面的对象数组管理的.代码遍历数组,然后,如果对象中的 .article 变量上有一个字符串,它会显示一个视图.如果字符串为空,则显示另一个.

the UIPageViewController class is set up to allow the user to swipe through the quotes. This is managed by the object array above. The code goes through the array and then, if there is a string on the .article var in the object, it shows one view. If the string is blank, it shows the other one.

我可以让程序交换视图,但计数器无法正常工作.这感觉就像一个简单的问题,但我无法弄清楚.

I can get the program to swap views, but the counter is not working correctly. This feels like a simple problem, but I'm not able to figure it out.

奖励:我的下一步是弄清楚如何让视图显示所选的引用/作者/文章.我也非常感谢任何关于此的提示,因为这对我来说都是新的.谢谢!

Bonus: My next step is to figure out how to have the view show the quote/author/article that is selected. I would also really appreciate any tips on that as well as this is all new to me. Thank you!

这是我的代码

import UIKit

var thePack:[packItem] = [packItem(theQuote: "quote1", author: "", article: "hasarticle3"),packItem(theQuote: "quote2", author: "", article: ""),packItem(theQuote: "quote3", author: "", article: "hasarticle3")]

var packIndex:Int = 0

class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

//set up the array of views to move to. All the different view types should be in here
lazy var subViewControllers:[UIViewController] = {
    return [
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "screen1") as! ViewController1,
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "screen2") as! ViewController2
    ]
}()
//end

override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
    self.dataSource = self

    //set up the initial viewController
    setViewControllers([subViewControllers[0]], direction: .forward, animated: true, completion: nil)
}


//set up the before view
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    packIndex -= 1
    if(packIndex <= 0){
        return nil
    }
    else{
        if thePack[packIndex].article != ""{
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "screen1") as! ViewController1
        }

        else{
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "screen2") as! ViewController2
        }
    }
}

//set up the after view
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    packIndex += 1

    if(packIndex >= thePack.count){
        return nil
    }
    else{
        if thePack[packIndex].article != ""{
            return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "screen1") as! ViewController1
        }

        else{
            return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "screen2") as! ViewController2
        }
    }
}

//change the transition style so it's not the page curl
required init?(coder: NSCoder) {
    super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
}


//MARK UIPageViewController DataSource
func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return subViewControllers.count
}

}

推荐答案

欢迎来到 SO!

首先你的方法是正确的,但主要问题是 UIPageViewController 中的那些方法没有像你预期的那样被调用,问题是如果你轻轻滚动一个视图控制器(所以它会向后滚动,而不是转到下一个位置)它会调用一个方法,可能是带有 viewControllerAfter 的方法,但是当视图向后滚动时它不会调用另一个方法,这会弄乱您的索引.我不是 100% 确定那是你的问题,但从实现来看似乎是这样,我的建议是为 UIPageViewController 中显示的所有视图控制器创建一个超类并保持那些页面索引.并使用控制器的页面来计算您的位置.像这样

First of all your approach is kind of right, but the main problem is that those methods from the UIPageViewController are not called as you might expect, the thing is if you gently scroll one view controller (so it will scroll back, not go to the next position) it will call one method probably the one with viewControllerAfter but it won't call the other one when the view scrolls back, which will mess up your index. I'm not 100% sure that that's your problem but from the implementation it seems so, What I suggest, is to create a super class for all your view controllers that are displayed in the UIPageViewController and hold the page index on those. And use the page form the controllers in order to compute your position. Something like this

class MyPageViewController: UIViewController {
    var page: Int = 0
}

class OneControllerThatWillBeDisplayed: MyPageViewController {
   // do adittional things here
}


class ThePageViewController ...delegates here {

//set up the before view
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    let theController = viewController as! MyPageViewController
    let theIndex = theController.page - 1
    if(theIndex < 0){
        return nil
    }
    else{
        if thePack[theIndex].article != ""{
            let theController =  UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "screen1") as? MyPageViewController
            theController.page = theIndex
            return theController
        }

        else{
            let theOtherOne  =  UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "screen2") as! MyPageViewController
            theOtherOne.page = theIndex
            return theOtherOne
        }
    }
}

//set up the after view
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    let theController = viewController as! MyPageViewController
    let theIndex = theController.page + 1
    if(theIndex >= thePack.count){
        return nil
    }
    else{
        if thePack[theIndex].article != ""{
            let theController =  UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "screen1") as? MyPageViewController
            theController.page = theIndex
            return theController
        }

        else{
            let theOtherOne  =  UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "screen2") as! MyPageViewController
            theOtherOne.page = theIndex
            return theOtherOne
        }
    }
}

最后,我的一个小建议,要小心 Apple 的自定义"控制器,大多数都非常糟糕且难以定制,在这一类别中如下:UIPageViewController, UITableViewController &UISplitViewController.在我看来,Apple 应该在这些控制器的原型设计方面做得更好,以帮助开发人员更多,但最终这只是我的意见

And in the end one small advice from my side, be careful with the "custom" controllers from Apple, most of the are really crapy and hard to customise, in this category are the following: UIPageViewController, UITableViewController & UISplitViewController. In my opinion Apple should do a better job in prototyping these controllers to help the developers more, but in the end that's only my opinion

这篇关于Swift 4 UIPageViewController 未显示正确的视图(按顺序在数组中定义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆