Lysdexia
Lysdexia

Reputation: 443

Present a ViewController that is embedded in Navigation Controller

I have a situation wherein a storyboard made VC(embedded in NavController) should be presented programmatically.

SomeVC -> presents NavController(rootVC) -> rootVC -> pushes subVC

on this representation, subVC should have a back button to go back to rootVC, but I can't implement it this way. Will be providing sample codes that I have already tried.

this pushes the rootVC directly:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"storyBoard" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"rootVC"];
[self.navigationController presentViewController:vc animated:YES completion:nil];

also tried pushing the navigationController itself, ID has been set on storyboard:

UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:@"navigationController"];
[self presentViewController:navController animated:YES completion:nil];

storyboard implementation

EDIT: storyboard implementation looks like this

Upvotes: 0

Views: 313

Answers (1)

I'm not sure if I understand you correctly. You want to present a navigation controller and have those rootVC and subVC already in it, right? So after presenting, you want the subVC to be presented with the back button right away.

If that's the case you need to tell the navigation controller to push the subVC before presenting it

UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:@"navigationController"];
UIViewController *subVC = [storyboard instantiateViewControllerWithIdentifier:@"subVC"];
[navController pushViewController:subVC animated:NO];
[self.navigationController presentViewController:vc animated:YES completion:nil];

Upvotes: 1

Related Questions