使用以下代码,我有一系列视图控制器嵌入在我浏览的页面视图控制器中。我无法通过按下按钮来找到一种方法来转换到另一个视图控制器。我计划为每个视图控制器中的每个条形按钮图标编写一个单独的函数。
class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
private var pages: [UIViewController]!
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
self.pages = [
self.storyboard!.instantiateViewControllerWithIdentifier("FirstNav") as! UINavigationController,
self.storyboard!.instantiateViewControllerWithIdentifier("SecondNav") as! UINavigationController,
self.storyboard!.instantiateViewControllerWithIdentifier("ThirdNav") as! UINavigationController,
self.storyboard!.instantiateViewControllerWithIdentifier("FourthNav") as! UINavigationController
]
let startingViewController = self.pages.first! as UIViewController
self.setViewControllers([startingViewController], direction: .Forward, animated: false, completion: nil)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let index = (self.pages as NSArray).indexOfObject(viewController)
// if currently displaying last view controller, return nil to indicate that there is no next view controller
return (index == self.pages.count - 1 ? nil : self.pages[index + 1])
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let index = (self.pages as NSArray).indexOfObject(viewController)
// if currently displaying first view controller, return nil to indicate that there is no previous view controller
return (index == 0 ? nil : self.pages[index - 1])
}
}
答案 0 :(得分:2)
将PageViewController
的实现更改为以下内容(我对已经实现的方法进行了一些更改,并添加了一个新的实例方法。)
class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
private var pages: [UINavigationController]!
private var currentPageIndex: Int!
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
self.pages = [
self.storyboard!.instantiateViewControllerWithIdentifier("FirstNav") as! UINavigationController,
self.storyboard!.instantiateViewControllerWithIdentifier("SecondNav") as! UINavigationController,
self.storyboard!.instantiateViewControllerWithIdentifier("ThirdNav") as! UINavigationController,
self.storyboard!.instantiateViewControllerWithIdentifier("FourthNav") as! UINavigationController
]
(self.pages[0].topViewController as! FirstViewController).parentPageViewController = self
(self.pages[1].topViewController as! SecondViewController).parentPageViewController = self
(self.pages[2].topViewController as! ThirdViewController).parentPageViewController = self
(self.pages[3].topViewController as! FourthViewController).parentPageViewController = self
self.currentPageIndex = 0
let startingViewController = self.pages.first! as UINavigationController
self.setViewControllers([startingViewController], direction: .Forward, animated: false, completion: nil)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let index = (self.pages as NSArray).indexOfObject(viewController)
self.currentPageIndex = index
// if currently displaying last view controller, return nil to indicate that there is no next view controller
return (self.currentPageIndex == self.pages.count - 1 ? nil : self.pages[self.currentPageIndex + 1])
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let index = (self.pages as NSArray).indexOfObject(viewController)
self.currentPageIndex = index
// if currently displaying first view controller, return nil to indicate that there is no previous view controller
return (index == 0 ? nil : self.pages[index - 1])
}
func displayPageForIndex(index: Int, animated: Bool = true) {
assert(index >= 0 && index < self.pages.count, "Error: Attempting to display a page for an out of bounds index")
// nop if index == self.currentPageIndex
if self.currentPageIndex == index { return }
if index < self.currentPageIndex {
self.setViewControllers([self.pages[index]], direction: .Reverse, animated: true, completion: nil)
} else if index > self.currentPageIndex {
self.setViewControllers([self.pages[index]], direction: .Forward, animated: true, completion: nil)
}
self.currentPageIndex = index
}
}
现在,在每个子视图控制器中,添加两段代码:
维护对父PageViewController
实例的弱引用的属性。
weak var parentPageViewController: PageViewController!
连接每个条形按钮项目的IBAction
。您可能需要根据Storyboard的设置方式更改此方法的主体。
@IBAction func barButtonTapped(sender: UIBarButtonItem) {
var newIndex = -1
switch sender.title! {
case "First":
newIndex = 0
case "Second":
newIndex = 1
case "Third":
newIndex = 2
case "Fourth":
newIndex = 3
default: break
}
self.parentPageViewController.displayPageForIndex(newIndex)
}
答案 1 :(得分:0)
每个控制器是否需要嵌入UINavigationController
?您可以只有UINavigationController
嵌入一个UIViewController
,该视图控制器引用pageviewcontroller
和barButtonItem
,当点击barButtonItem
触发一个操作时告诉pageviewcontroller
更改索引。
或者,如果您想使用每个barButtonItem
中UINavigationController
的当前行动计划,请使用委托方法didSelectBarButtonAtIndex
或其他内容编写协议,然后为每个{ {1}}委托属性,UINavigationController
符合协议并成为每个导航控制器的委托。然后,当点击一个条形按钮时,调用委托方法,这将使页面控制器改变它的索引。