6

I have a tab view controller which has a button like so and when it gets pressed a modal appears:

PostViewController *post = [[PostViewController alloc] init];

// [self.navigationController pushViewController:post animated:YES];

// Presentation
[self presentViewController:post animated:YES completion:nil];

When the modal is done I want to dismiss it and push a new view controller like so:

ProfilesViewController *profile = [[ProfilesViewController alloc] init];
[self.navigationController pushViewController:profile animated:YES];

But I can't do it in the post vc as its a modal. How do I do this?

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
cdub
  • 24,555
  • 57
  • 174
  • 303

2 Answers2

7

You can try using completionBlock.

CompletionBlock is called when presentViewController is done.

PostViewController *post = [[PostViewController alloc] init];
[con presentViewController:post animated:YES completion:^{
    ProfilesViewController *profile = [[ProfilesViewController alloc] init];
    [self.navigationController pushViewController:profile animated:YES];
}];

More information about presentViewController:animated:completion: Apple Doc

completion : The block to execute after the presentation finishes. This block has no return value and takes no parameters. You may specify nil for this parameter.

Kevin Machado
  • 4,141
  • 3
  • 29
  • 54
  • Okay so it pushes it behind the modal? – cdub Oct 09 '14 at 08:17
  • It pushes into the navigationController stack. – Kevin Machado Oct 09 '14 at 08:19
  • 1
    If you want dismiss your modal before, you can add `dismissViewControllerAnimated:completion:` using the completionBlock too – Kevin Machado Oct 09 '14 at 08:22
  • One more question, once I dismiss post can the post data be displayed in the ProfilesViewController? So does the profile view controller run when it becomes visible? – cdub Oct 09 '14 at 08:23
  • When you dismiss a ViewController, it `releases all data` inside. So you must provide all args you want to keep to the new VC. Hope this answer your question – Kevin Machado Oct 09 '14 at 08:29
1

Dose your tab view controller embedded in a UINavigationController? If you have not, you of course cannot use self.navigationController.

Steve Lai
  • 633
  • 1
  • 7
  • 18