2017-06-12 46 views
0

我想重用代碼,所以我創建redirectToTabBarControllerChild斯威夫特:重用代碼傳遞類參數

func redirectToTabBarControllerChild(storyboardName: String, tabBarIndex: Int, segueID: String, viewController: UITableViewController) { 
    let storyboardMain = UIStoryboard(name: storyboardName, bundle: nil) 
    let tabBarController = storyboardMain.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController 
    window?.rootViewController = tabBarController 
    tabBarController.selectedIndex = tabBarIndex 
    let navigationViewContrller = tabBarController.childViewControllers[tabBarIndex] as! UINavigationController 
    let currentViewContrller = navigationViewContrller.childViewControllers.first as! viewController 
    currentViewContrller.performSegue(withIdentifier: segueID, sender: nil) 
    window?.makeKeyAndVisible() 
} 

最後一個參數,我想傳遞ProfileViewController例如。但我有一個錯誤在這行:let currentViewContrller = navigationViewContrller.childViewControllers.first as! viewController

Use of undeclared type viewController

有誰知道我怎麼能做到這一點?

+0

,因爲它是一個對象,而不是一個類型。試試看'viewController.self'而不是 –

回答

1

navigationController?.childViewControllersUIViewControllers一個array。你獲得first屬性,這意味着你需要將它投射到as! UIViewController,這將強制施放它,它不會是一個可選項。

你想要做的是將其轉換爲UITableViewController,所以像這樣做:

if let vc = navigationViewContrller.childViewControllers.first as? UITableViewController { 
    // Will succeed if the first is of type UITableViewController 
    // Use vc in here 
} 
+0

謝謝你的回答,但我認爲它是不可重用的,因爲如果使用UIViewController,例如我使用UITableViewController,如何在參數中傳遞這個參數? –

+0

@VictorMendes,檢查更新的答案。 –

+0

謝謝!有效! –

0

替換的viewController到的UITableViewController