-1
我需要代碼爲UIpageViewcontroller,它將像下面的MVC的圖像工作。UIPage ViewController,我想從非uipage視圖控制器導航另一個第二viewcontroller
編輯:
當我點擊 「其他視圖控制器」 的下一個按鈕,它會調用UIPageViewcontroller的視圖控制器2。 上圖中顯示了確切的流程。
我需要代碼爲UIpageViewcontroller,它將像下面的MVC的圖像工作。UIPage ViewController,我想從非uipage視圖控制器導航另一個第二viewcontroller
編輯:
當我點擊 「其他視圖控制器」 的下一個按鈕,它會調用UIPageViewcontroller的視圖控制器2。 上圖中顯示了確切的流程。
確定這是做
首先你需要在故事創造一個UIPageViewController和類設置爲相同的代碼
的一個新的快捷文件複製並粘貼此
的一種方式class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
//make sure the storyboard PageviewControllers class is set to this class or it wont work
lazy var VCArray: [UIViewController] = {
//here is where your viewController pages go. in storyboard create multiple viewControllers and give them a storyboard ID here is where the storyboard IDs will be put in
return [self.VCInstance(name: "firstVC"),
self.VCInstance(name: "secondVC"),
self.VCInstance(name: "thirdVC")]
}()
private func VCInstance(name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
}
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
if let firstVC = VCArray.first {
setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
for view in self.view.subviews {
if view is UIScrollView {
view.frame = UIScreen.main.bounds
} else if view is UIPageControl {
view.backgroundColor = UIColor.clear
}
}
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = VCArray.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return VCArray.last
}
guard VCArray.count > previousIndex else {
return nil
}
return VCArray[previousIndex]
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = VCArray.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
guard nextIndex < VCArray.count else {
return VCArray.first
}
guard VCArray.count > nextIndex else {
return nil
}
return VCArray[nextIndex]
}
//this is optional for the page counter at the bottom
public func presentationCount(for pageViewController: UIPageViewController) -> Int {
return VCArray.count
}
//this is optional for the page counter at the bottom
public func presentationIndex(for pageViewController: UIPageViewController) -> Int {
guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = VCArray.index(of: firstViewController) else {
return 0
}
return firstViewControllerIndex
}
}
Explanation-
上面的代碼是非常可定製的,你可以改變是否你有一個頁面計數器在底部或多少頁面顯示通過使用它將多個故事板viewControllers成爲一個單頁面的viewController,因爲這些頁面是單獨的故事板viewControllers你可以把任何你想要的網頁....如果你仍然困惑看看這個視頻上YouTube
導航到不同的視圖控制器工作良好,但我需要,當我們在「其他ViewController」它不會在UIPageViewController的成員(如ViewController 1,2,3)中,如果我們單擊下一個按鈕,視圖控制器將調用pageviewcontroller子視圖(ViewController 2)。只需在我的問題帖子中查看上面的圖片。 –
你需要做的是使用上面的代碼和第一個pageViewController的第二頁上的兩個pageViewControllers放置一個UIButton,導航到第二個pageViewController的第一頁,如果這是有道理的 – Anonymous