2016-07-24 37 views
1

我目前正在開發一個iOS應用程序,使用CarbonKit庫進行導航。我目前在第一個選項卡中有一個UITableView,我希望在選擇單元格時以編程方式導航到另一個選項卡。Swift - 如何從特定實例調用方法?

我已實現了以下功能下我主CarbonKit視圖控制器:

import CarbonKit 

class ViewController: UIViewController, CarbonTabSwipeNavigationDelegate { 


    func carbonTabSwipeNavigation(carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAtIndex index: UInt) -> UIViewController  { 

     switch index { 
     case 0: 
      return self.storyboard!.instantiateViewControllerWithIdentifier("AircraftViewController") as! AircraftViewController 
     case 1: 
      return self.storyboard!.instantiateViewControllerWithIdentifier("LoadController") as! LoadController 
     case 3: 
      return self.storyboard!.instantiateViewControllerWithIdentifier("SettingsController") as! SettingsController 
     default: 
      return self.storyboard!.instantiateViewControllerWithIdentifier("ViewControllerOne") as! ViewControllerOne 
     } 
    } 

    func navigate(index: UInt) { 
     carbonTabSwipeNavigation.setCurrentTabIndex(index, withAnimation: true) 
    } 

} 

setCurrentTabIndex是由庫以編程切換標籤提供的功能。

在我UITableViewController我有以下幾點:

import UIKit 
import CarbonKit 

class SecondViewController: UITableViewController { 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     \\ First Attempt 
     (ViewController() as CarbonTabSwipeNavigationDelegate).navigate(2) 

     \\ Second Attempt 
     let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("controller") as! ViewController 
     viewController.navigate(2) 
    } 

} 

據我所知,我的第一次嘗試,我創建的ViewController一個新實例,我的第二次嘗試沒有選擇我所期待的實例。

如何從SecondViewController開始在ViewController的第一個實例中最好地激活該方法?

+0

您需要堅持對您創建的「CarbonTabSwipeNavigation」的引用。我猜你在你的應用程序委託中設置了一切。你在哪裏實現'carbonTabSwipeNavigation:viewControllerAtIndex'協議方法?我懷疑這對於爲每個子視圖控制器使用'CarbonTapSwipeNavigation'設置一個屬性是個好地方。然後你可以在你的'SecondViewController'中使用這個屬性。 – dylansturg

+0

我在上面的'viewController'類中實現'viewControllerAtIndex'。如何將viewController設置爲屬性?我已經用前面提到的'viewControllerAtIndex'方法更新了我的問題。 – user3185748

回答

1

開始加入了CarbonTabSwipeNavigation屬性您SecondViewController類,所以你將有你的導航提供接入:

import UIKit 
import CarbonKit 

class SecondViewController: UITableViewController { 
    weak var carbonNavigation : CarbonTabSwipeNavigation?; 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     if let carbonNav = self.carbonNavigation { 
      carbonNav.setCurrentTabIndex(index, withAnimation: true) 
     } 
    } 

} 

接下來,我們需要設置carbonNavigation屬性,所以這將是可供使用。它看起來像你的CarbonTabSwipeNavigationDelegate是一個很好的去處。在附註中,我沒有看到您的代表已經返回SecondViewController - 是否有意?

import CarbonKit 

class ViewController: UIViewController, CarbonTabSwipeNavigationDelegate { 


    func carbonTabSwipeNavigation(carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAtIndex index: UInt) -> UIViewController  { 

     switch index { 
     case 0: 
      let aircraftVC = self.storyboard!.instantiateViewControllerWithIdentifier("AircraftViewController") as! AircraftViewController 
      aircraftVC.carbonNavigation = carbonTabSwipeNavigation 
      return aircraftVC 
     case 1: 
      // do something similar, instantiate view controller, set navigation property 
      return self.storyboard!.instantiateViewControllerWithIdentifier("LoadController") as! LoadController 
     case 3: 
      // do something similar, instantiate view controller, set navigation property 
      return self.storyboard!.instantiateViewControllerWithIdentifier("SettingsController") as! SettingsController 
     default: 
      // do something similar, instantiate view controller, set navigation property 
      return self.storyboard!.instantiateViewControllerWithIdentifier("ViewControllerOne") as! ViewControllerOne 
     } 
    } 

    func navigate(index: UInt) { 
     carbonTabSwipeNavigation.setCurrentTabIndex(index, withAnimation: true) 
    } 
} 

你也可以添加一個ViewController屬性,而不是一個CarbonTabSwipeNavigation,揭露所有從碳需要按照ViewController方法的方法。這意味着更改財產聲明的類型並將其設置爲self而不是carbonTabSwipeNavigation

+0

好像你可能想'弱變量'來避免強參考週期 –

+0

對於最初的文章,爲簡單起見,我將'AircraftViewController'類的名稱更改爲'SecondViewController',並忘記在更新我的文章時更改它。否則,這是我所希望的。我並不期望在第二個視圖控制器中聲明我的變量。你有沒有推薦用於學習可可和Xcode與Swift一起復雜的資源?再次感謝您的幫助! – user3185748

+0

@AaronBrager好點。將更新我的代碼。 – dylansturg