2015-07-11 96 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

相關問題