2015-07-11 55 views
3

使用下面的代码,我有一系列嵌入在页面视图控制器中的视图控制器,我正在浏览。我只是无法找到一种方式通过按下按钮来继续到另一个视图控制器。我打算为每个视图控制器中的每个栏按钮图标编写一个单独的函数。在页面视图控制器中更改VC的索引(Swift)

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

您尝试Segue公司从视图控制器其视图控制器,并把栏按钮,你在哪里尝试? – Frankie

+0

上面的代码是页面视图控制器的代码。由于按钮代码有不同的按钮,因此条形码代码会进入所有子视图控制器。我需要从FirstNav继续ThirdNav等等。 – chicobermuda

回答

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 
    } 

} 

现在,在每个子视图控制器,添加两段代码:

  1. 一个属性,该属性保留对父实例PageViewController实例的弱引用。

    weak var parentPageViewController: PageViewController! 
    
  2. 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) 
    } 
    
+0

因为我知道哪个按钮对应哪个页面,所以我放弃了整个'switch'语句。 '.displayPageForIndex()'方法取得了诀窍。非常感谢! – chicobermuda

+1

@dperk不客气。我很高兴我能帮上忙! – ndmeiri

+0

假设我有一个导航控制器阵列,就像我目前所做的一样,但也是页面视图控制器之外的一个单独的视图控制器。由于我不能直接从页面视图控制器中继续,是否有可能在页面视图控制器中实例化这个单独的VC,并从其他vc切换到现任阵列的导航控制器? @ndmeiri – chicobermuda

0

是否每个控制器都需要嵌入UINavigationController?您可以只有一个UINavigationController嵌入单个UIViewController,该视图控制器参考pageviewcontrollerbarButtonItem,当轻击barButtonItem触发动作,告诉pageviewcontroller更改索引。

或者,如果你想要去与你目前的行动计划是在每个UINavigationController一个barButtonItem,写有委托方法didSelectBarButtonAtIndex什么协议,然后给每个UINavigationController的委托财产,有pageviewcontroller符合协议并成为每个导航控制器的代表。然后,当点击一个条形按钮时,调用委托方法,这会使页面控制器更改它的索引。

+0

是的,每个视图控制器都有自己的导航控制器。我将每个栏按钮添加到每个视图控制器中的自定义视图中,而不是导航控制器。 – chicobermuda

相关问题