2016-11-25 160 views
1

我有一個UIViewController嵌入UINavigationController。我想介紹這viewController和呈現後,我也需要導航方法(推&波普),所以我提出UINavigationController而不是UIViewController,但爲此,我需要傳遞自定義數據到ViewController作爲我沒有訪問該ViewController形式以前ViewController。 這是我的代碼。通過NavigationController傳遞數據

UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyNavigationIdentity"]]; 

[self presentViewController:navigationController animated:YES completion:nil]; 

P.S我不能allocinitnavigationController爲我設計我的navigationController從故事板。

+0

爲此,您需要使用'UINavigationController'的childViewControllers屬性。你可以查看我的答案瞭解更多詳情 – KrishnaCA

回答

4

您可以訪問的UINavigationControllerviewControllerviewControllers屬性只是訪問ViewController嵌入您的UINavigationController內。

if (navigationController.viewControllers.count > 0) { 
    //Cast UIViewController with your custom Controller 
    ViewController *vc = (ViewController*) [navigationController.viewControllers firstObject]; 
    //Now pass data you want 
    vc.passData = ... 
} 
3

您可以使用childViewControllers財產

NSArray *viewControllers = [navigationController childViewControllers]; 
if (viewControllers.count > 0) { 
    UIViewController *myViewController = (UIViewController *)[viewControllers firstObject]; 
} 
+0

不客氣:) – KrishnaCA