包含模型数据的容器视图中的页面视图控制器

Posted

技术标签:

【中文标题】包含模型数据的容器视图中的页面视图控制器【英文标题】:Page View controller in container view with data from model 【发布时间】:2017-03-30 17:10:55 【问题描述】:

我有一个显示页面视图控制器的容器视图。这个视图控制器然后显示 2 个相同的视图控制器,我可以来回翻页。

        import UIKit

class ViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate 

var pages = [UIViewController]()

override func viewDidLoad() 
super.viewDidLoad()

self.delegate = self
self.dataSource = self

let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page1")
let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "page2")

pages.append(page1)
pages.append(page2)

setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)


func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? 
let currentIndex = pages.index(of: viewController)!
let previousIndex = abs((currentIndex - 1) % pages.count)
print("previous Index", pages[previousIndex])
return pages[previousIndex]


func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? 
let currentIndex = pages.index(of: viewController)!
let nextIndex = abs((currentIndex + 1) % pages.count)
print("next Index", pages[nextIndex])
return pages[nextIndex]


func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int 
return pages.count


func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int 
return 0


我想做的是只创建一个视图控制器,然后用数组中的数据填充该视图控制器

数组:

var pageTitles : Array<String> = ["God vs Man", "Cool Breeze", "Fire Sky"]
var pageImages : Array<String> = ["page1.png", "page2.png", "page3.png"]

如果容器视图控制器呈现页面视图控制器,这是否可能?

任何帮助将不胜感激......

丹尼斯·安吉尔

【问题讨论】:

【参考方案1】:

如果您只想管理一个具有多数据和UIPageController 的控制器,您应该创建一个视图控制器并执行类似的操作:

class PageViewController: UIViewController  // Id: 'PageViewController'
    // MARK: @IBOutlet

    /// Menu button item.
    @IBOutlet weak var image: UIImageView!

    /// Pagination controller.
    @IBOutlet weak var pagination: UIPageControl!

    // MARK: Variable
    struct Page 
        var title: String
        var image: String
        init(title: String, image: String) 
            self.title = title
            self.image = image
        
    
    let data: [Page] = []
    var currentIndex = 0

    override func viewDidLoad() 
        // Init data
        data.append(Page(title: "title1" , image: "page1.png"))
        data.append(Page(title: "title2" , image: "page2.png"))

        setValues(currentIndex)

        // Setup slide handler
        let leftSwipe = UISwipeGestureRecognizer(target: self,
                                                 action: #selector(PageViewController(_:)))
        let rightSwipe = UISwipeGestureRecognizer(target: self,
                                                  action: #selector(PageViewController(_:)))
        leftSwipe.direction = .left
        rightSwipe.direction = .right
        self.view.addGestureRecognizer(leftSwipe)
        self.view.addGestureRecognizer(rightSwipe)
    

    /**
     Handle swipes allows to switch page.
     */
    func handleSwipes(_ sender: UISwipeGestureRecognizer) 
        let size = self.data.count
        let currentPosition = self.pagination.currentPage

        if sender.direction == .left && currentPosition < size-1 
            nextPage()
         else if sender.direction == .right && currentPosition > 0 
            previousPage()
        
    

    private func setValues(index: Int) 
        self.pagination.currentPage = currentIndex
        self.title = pageTitles[index]
        self.image = pageImages[index]
    

    // Button or see UIPageControl @IBOutlet
    private func nextPage() 
        currentIndex = abs((currentIndex + 1) % pages.count)
        print("next Index", pages[currentIndex])
        setValues(currentIndex)
    

    // Button or see UIPageControl @IBOutlet
    private func previousPage() 
        currentIndex = abs((currentIndex - 1) % pages.count)
        print("next Index", pages[currentIndex])
        setValues(currentIndex)
    

    // Other functions...
    // ...


【讨论】:

以上是关于包含模型数据的容器视图中的页面视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

如何将 nib 文件中的 UITableView 加载到父视图控制器中的容器视图中?

从容器视图中的选项卡栏控制器中的视图控制器委派信息

在嵌入在两个独立容器控制器中的视图之间实现委托

将数据从容器视图中的 tableview 中的静态单元格发送到 Swift 中的视图控制器

为啥我的用户数据没有使用资源控制器中的 show 方法传递到页面视图?

将视图模型的一部分传递给控制器