David Casillas
David Casillas

Reputation: 1921

How to present the same modalView after dismissing it once

I'm having some problem trying to present a modal view controller after it has been presented the first time, so I just start a little test method, it presents, dismisses and presents again the same controller modally.

// This is just test Code.
MYViewController *vc = [[MYViewController alloc] init];
[self presentModalViewController:vc animated:YES];
[self dismissModalViewControllerAnimated:YES];
[self  presentModalViewController:vc animated:YES];

I get the error:

2011-11-15 09:50:42.678 Proyecto3[1260:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <RootViewController: 0x6d9d090>.'

The documentation does not add any clue here.

Upvotes: 0

Views: 1057

Answers (3)

Prashant Rane
Prashant Rane

Reputation: 436

@David, make the MYViewController an instance variable, and initialize it like this:

if (myInstance==nil)
     //create instance of MYViewController
     //myInstance.delegate=self
//present modal VC

In MYViewController create a protocol to co-ordinate dismissing MYViewController may be on a done or cancel button. In the button action call some thing like

    done 
    {
       if([delegate respondsToSelector:@selector(willDismissModalView)])
       {
           [delegate willDismissModalView];
       }
   }

and in willDismissModalView method of your VC dismiss MYViewController. This way you can do it 'n' times.

Upvotes: 1

jbat100
jbat100

Reputation: 16827

You cannot present/dismiss the view controller while it is animated, I think this works

MYViewController *vc = [[MYViewController alloc] init];
[self presentModalViewController:vc animated:NO];
[self dismissModalViewControllerAnimated:NO];
[self  presentModalViewController:vc animated:YES];

But I don't really see any reason for doing it, why would you want to dismiss and re-present a modal view controller which is already presented?

Upvotes: 0

Sree
Sree

Reputation: 907

In your code [self dismissModalViewControllerAnimated:YES]; will do nothing to the modalViewController,since here the "self" is the viewController from where you are trying to present a modalViewController "vc".Again you are presenting a modalViewController which is already presented.this will result in a termination.

You can dismiss the modalViewController vc in that viewController,here vc.

Upvotes: 0

Related Questions