Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift3 容器视图中具有模型数据的页面视图控制器_Swift3_Uipageviewcontroller - Fatal编程技术网

Swift3 容器视图中具有模型数据的页面视图控制器

Swift3 容器视图中具有模型数据的页面视图控制器,swift3,uipageviewcontroller,Swift3,Uipageviewcontroller,我有一个显示页面视图控制器的容器视图。这个视图控制器显示了两个相同的视图控制器,我可以前后翻页 import UIKit class ViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate { var pages = [UIViewController]() override func viewDidLoad() { super.vi

我有一个显示页面视图控制器的容器视图。这个视图控制器显示了两个相同的视图控制器,我可以前后翻页

        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"]
var-pageTitles:Array=[“上帝vs人”、“凉风”、“火天”]
var pageImages:Array=[“page1.png”、“page2.png”、“page3.png”]
对于呈现页面视图控制器的容器视图控制器,这可能吗

任何帮助都将不胜感激


Denis Angell

如果您只想使用多数据和
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...
    // ...

}
class PageViewController:UIViewController{//Id:'PageViewController'
//马克:@IBOutlet
///菜单按钮项。
@IBVAR图像:UIImageView!
///分页控制器。
@IBOutlet弱变量分页:UIPageControl!
//标记:变量
结构页{
变量标题:字符串
变量图像:字符串
初始化(标题:字符串,图像:字符串){
self.title=标题
self.image=image
}
}
let data:[页面]=[]
var currentIndex=0
重写func viewDidLoad(){
//初始数据
data.append(页面(标题:“title1”,图片:“page1.png”))
data.append(页面(标题:“title2”,图片:“page2.png”))
设置值(当前索引)
//设置幻灯片处理程序
让leftSwipe=UISwipegestureRecognitor(目标:self,
操作:#选择器(PageViewController(#:))
让rightSwipe=UISwipegestureRecognitor(目标:self,
操作:#选择器(PageViewController(#:))
leftSwipe.direction=.left
rightSwipe.direction=.right
self.view.addgestureRecognitor(左击)
self.view.addgestureRecognitor(右击)
}
/**
手柄滑动允许切换页面。
*/
func HandleSweeps(uu发送方:UISweepGestureRecognitizer){
让大小=self.data.count
让currentPosition=self.pagination.currentPage
如果sender.direction==.left&¤tPosition0,则为else{
上一页()
}
}
私有函数集值(索引:Int){
self.pagination.currentPage=currentIndex
self.title=页面标题[索引]
self.image=页面图像[索引]
}
//按钮或参见UIPageControl@IBOutlet
私有函数下一页(){
currentIndex=abs((currentIndex+1)%pages.count)
打印(“下一个索引”,页面[currentIndex])
设置值(当前索引)
}
//按钮或参见UIPageControl@IBOutlet
私有函数上一页(){
currentIndex=abs((currentIndex-1)%pages.count)
打印(“下一个索引”,页面[currentIndex])
设置值(当前索引)
}
//其他功能。。。
// ...
}

如果只想使用多数据管理一个控制器和
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...
    // ...

}
class PageViewController:UIViewController{//Id:'PageViewController'
//马克:@IBOutlet
///菜单按钮项。
@IBVAR图像:UIImageView!
///分页控制器。
@IBOutlet弱变量分页:UIPageControl!
//标记:变量
结构页{
变量标题:字符串
变量图像:字符串
初始化(标题:字符串,图像:字符串){
self.title=标题
self.image=image
}
}
let data:[页面]=[]
var currentIndex=0
重写func viewDidLoad(){
//初始数据
data.append(页面(标题:“title1”,图片:“page1.png”))
data.append(页面(标题:“title2”,图片:“page2.png”))
设置值(当前索引)
//设置幻灯片处理程序
让leftSwipe=UISwipegestureRecognitor(目标:self,
操作:#选择器(PageViewController(#:))
让rightSwipe=UISwipegestureRecognitor(目标:self,
操作:#选择器(PageViewController(#:))
leftSwipe.direction=.left
rightSwipe.direction=.right
self.view.addgestureRecognitor(左击)
self.view.addgestureRecognitor(右击)
}
/**
手柄滑动允许切换页面。
*/
func HandleSweeps(uu发送方:UISweepGestureRecognitizer){
让大小=self.data.count
让currentPosition=self.pagination.currentPage
如果sender.direction==.left&¤tPosition0,则为else{
上一页()
}
}
私有函数集值(索引:Int){
self.pagination.currentPage=currentIndex
self.title=页面标题[索引]
self.image=页面图像[索引]
}
//按钮或参见UIPageControl@IBOutlet
私有函数下一页(){
currentIndex=abs((currentIndex+1)%pages.count)
打印(“下一个索引”,页面[currentIndex])
设置值(当前索引)
}
//按钮或参见UIPageControl@IBOutlet
私有函数上一页(){
currentIndex=abs((currentIndex-1)%pages.count)
打印(“下一个索引”,页面[currentIndex])
设置值(当前索引)
}
//其他功能。。。
// ...
}