我目前正在使用CarbonKit库开发iOS应用程序进行导航。我目前在第一个标签中有UITableView
,我想在选择单元格时以编程方式导航到另一个标签。
我在我的主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
的新实例,而我的第二次尝试并没有选择我希望的实例。
如何在ViewController
的{{1}}的第一个实例中最好地激活方法?
答案 0 :(得分: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
。