2017-04-15 49 views
0

我目前雨燕3 創建一個登錄項目,我有一個包含如何訪問同一實例內的另一個類的函數?

  1. 的容器,它包括一個頁面視圖控制器和多頁的主視圖控制器
  2. 定製的「菜單」底部它包括兩個按鈕用於導航頁面視圖控制器和自定義頁面控制

This is my storyboard setup

用戶將p上的視圖不滿是由頁面視圖控制器控制的頁面和頂部的控制菜單構成,底部是控制菜單。當用戶在控制菜單中點擊按鈕的代碼將在主視圖 - 控制執行:

@IBAction func buttonNext(_ sender: UIButton) { 
    print("buttonNext has been pressed!") 
    nextPageWithIndex(index: nextIndex) 
} 

,這裏是它調用的函數:

func nextPageWithIndex(index: Int) { 

     let nextWalkthroughVC = pages[index] 
     pageContainer.setViewControllers([nextWalkthroughVC], direction: .forward, animated: true, completion: nil) 
     print("nextPageWithIndex has been executed with Index \(index)!") 

     if currentIndex as Int? == 2 { 
      currentIndex = 2 
     } else if currentIndex as Int? == 1 { 
      currentIndex = 2 
     } else if currentIndex as Int? == 0 { 
      currentIndex = 1 
     } else { 
      currentIndex = 0 
      print("currentIndex was in inpossible state!") 
     } 

     checkButtonNavigation() 
     assignIndexes() 

     if let index = currentIndex { 
      pageControl.currentPage = index 
      print("PageControl updated") 
     } 
} 

checkButtonNavigation()檢查是否在按鈕底部應根據當前正在顯示的頁面顯示。


我試圖實現一個頁面的按鈕(在這種情況下page1)導航到下一頁的用戶。

我試圖與varAccountRegisterMainViewController.nextPageWithIndex(index: 1)上在第1頁的ViewController類按壓按鈕的動作通過定義要執行的函數nextPageWithIndex(index: 1)

var varAccountRegisterMainViewController: accountRegisterMainViewController! 

在AppDelegate中, 和:

varAccountRegisterMainViewController = accountRegisterMainViewController() 

在page1 viewcontroller。

可悲的是,似乎沒有工作。我碰到一個「致命的錯誤:索引超出範圍」的觀點:

let nextWalkthroughVC = pages[index] 

在主視圖 - 控制功能nextPageWithIndex(index: 1)。我檢查了數組「頁面」的數量。它是0.但是,當我從主視圖控制器執行該功能時,它應該有3個。 Here is the error as an image.

謝謝你的幫助!


編輯:

這裏是頁陣是如何設置:

在主視圖 - 控制類的開頭添加以下代碼:

var pages = [UIViewController]() 

在viewDidLoad()中代碼:

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let page1: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage1ViewController") 
let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage2ViewController") 
let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage3ViewController") 
pages.append(page1) 
pages.append(page2) 
pages.append(page3) 

ANOTHER編輯:

我把 「頁面」 也進入externalNext()函數的聲明。現在的問題是解決了,但還有另外一個......這就是函數現在的樣子:

func externalNext(index: Int) { 

    var pageContainer: UIPageViewController! 

    var pages = [UIViewController]() 

    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let page1: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage1ViewController") 
    let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage2ViewController") 
    let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage3ViewController") 
    pages.append(page1) 
    pages.append(page2) 
    pages.append(page3) 

    // Create the page container 
    pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil) 
    pageContainer.delegate = self 
    pageContainer.dataSource = self 
    pageContainer.setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil) 

    // Add it to the view 
    view.addSubview(pageContainer.view) 
    pageContainer.view.addSubview(pageControlView) 

    print("Current array count of pages is \(pages.count)") 
    print("Function: nextPageWithIndex has been called with index \(index)") 
    let nextWalkthroughVC = pages[index] 
    pageContainer.setViewControllers([nextWalkthroughVC], direction: .forward, animated: true, completion: nil) 
    print("nextPageWithIndex has been executed with Index \(index)!") 

    pageControl.currentPage = index 
    print("PageControl updated") 

    print("Hello") 
} 

我從主視圖控制器類的頂部複製的代碼,只是它粘貼到函數。不過,我總是得到錯誤「致命錯誤:意外地發現零,同時解開可選值」。

有什麼辦法可以重新定義函數中的對象嗎? 或者有沒有優雅的方式?我的方式看起來很混亂...

感謝Phillip Mills我知道這與作爲類的主視圖控制器的另一個實例有關。我如何訪問那裏的函數,但在同一個實例中?主視圖控制器始終可見。

謝謝!

+0

「頁面」數組在'accountRegisterMainViewController'中如何設置? –

+0

我剛更新了這個問題。謝謝! – wyte

回答

0

當您這樣做:accountRegisterMainViewController()您正在創建一個新對象,該類的另一個實例。由於沒有顯示新文件,所以從未調用viewDidLoad,並且您的數組爲空。

您需要安排對象之間的鏈接,以便從故事板加載的對象也是nextPageWithIndex所調用的對象。

+0

哇!我不知道它會創建該類的新實例。這對我來說也是一個問題。你將如何繼續我的處境?你是什​​麼意思的鏈接? – wyte

+0

我的意思是你的頁面控制器中的一個變量指向從故事板發出的主控制器。通過查看加載到窗口的根視圖控制器中的內容,您應該可以在應用程序委託中執行此操作。 –

+0

請問你能提供一些代碼嗎?我對編程相當陌生。你會拯救我的一天! – wyte

相關問題