Syed Qamar Abbas
Syed Qamar Abbas

Reputation: 3677

Passing Data Via NavigationController

I have a UIViewController embed in with a UINavigationController. I want to present this viewController and after presenting I also need Navigation method (Push & Pop) so I am presenting UINavigationController instead of UIViewController but for that I need to pass custom data to that ViewController as of I have no access to that ViewController form previous ViewController. Here is my code.

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

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

P.S I cannot alloc and init new navigationController as I am designing my navigationController from Storyboard.

Upvotes: 1

Views: 1131

Answers (2)

Nirav D
Nirav D

Reputation: 72420

You can access your viewController from viewControllers property of 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 = ...
}

Upvotes: 4

KrishnaCA
KrishnaCA

Reputation: 5695

You can simply access the ViewController embed inside your UINavigationController using the childViewControllers property

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

Upvotes: 4

Related Questions