Swift - 如何从特定实例调用方法?

Swift - How to call method from a specific instance?

我目前正在开发一个 iOS 应用程序,使用 CarbonKit 库进行导航。我目前在第一个选项卡中有一个 UITableView,我想在一个单元格被 selected 时以编程方式导航到另一个选项卡。

我在主 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 的新实例,而我的第二次尝试没有 select 我希望的实例。

如何最好地激活 SecondViewControllerViewController 的第一个实例中的方法?

首先将 CarbonTabSwipeNavigation 属性 添加到您的 SecondViewController class,这样您就可以访问您的导航提供商:

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 并将您需要的所有 Carbon 方法公开为 ViewController 中的方法。这意味着更改 属性 声明的类型并将其设置为 self 而不是 carbonTabSwipeNavigation.