objective-c - PageViewController 当前索引不正确

标签 objective-c swift

我试图在用户滑动到最后一个 View Controller (4/4)后显示一个按钮。但是索引错误地输出了错误的数字(即使显示了正确的 View Controller )。

func viewControllerAtIndex(index : Int) -> UIViewController? {
    if((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
        return nil
    } 
    let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController

    pageContentViewController.imageName = self.images[index]
    pageContentViewController.titleText = self.pageTitles[index]
    pageContentViewController.pageIndex = index


    println(pageContentViewController.imageName)
    if index  == 3 { // show start button once user reaches the last screen (index = 3)
        startBtn.hidden = false
    } else {
        startBtn.hidden = true
    }

    return pageContentViewController
}

所有 View 都正确显示,除了索引从 1、2、4、2 随机跳转。如果我来回滑动,情况会变得更糟。

为什么索引没有显示正确的索引(与显示的 View Controller 一致)?

entire code

最佳答案

如果您对 viewControllerAtIndex 的使用类似于 Xcode 为“基于页面的应用程序”提供的项目模板,则最终会从 UIPageViewControllerDataSource 的方法之一调用该模板>,即 pageViewController:viewControllerAfterViewControllerpageViewController:viewControllerBeforeViewController。页面 View Controller 调用这些方法的时间和提供的索引完全取决于其内部实现,可能与您导航和查看页面的顺序无关。我认为它针对内存消耗和性能进行了高度优化,因此它要求 View Controller 能够满足这些目标。

关于objective-c - PageViewController 当前索引不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30698762/

相关文章:

ios - Swift 在不同 View 中捕获异步函数

swift - 如何在 swift 中将数据转换为十六进制字符串

objective-c - 在 Objective C 方法中创建 UIButton

ios - 如何识别屏幕高五

swift - 类型 'String.Index' 不符合协议(protocol) '_Strideable'

iphone - 两个 NSDate 之间的区别

ios - 'NSString !' is not convertible to ' 字符串';您的意思是使用 'as!' 强制向下转换吗?

ios - 使用自定义对象保存 NSMutableArray 时出现问题

objective-c - Cocoapods 问题 : "How does cocoapods know when a new version of a library is available?" etc

objective-c - objective-c : Initializing static variable with static method call