4

If I'm not mistaken, modal views have to be dismissed from the parent view, not from the modal view itself.

In my current project I have two modal views. In the first one, I pass data to the parent view. When the data is passed to the parent view, the dismiss is executed.

Now, I have another modal view that doesn't pass data to the parent view, so I don't know how to dismiss other than doing one self dismissModalView

Other than that, any other suggestion for a good practise on this topic?

Thanks in advance!

UPDATE:

From the answers I´m getting, I see I haven´t make myself very clear (not unusual, BTW).

I know how to self dismiss a modalViewController. That´s no problem. I also know how to use the protocol-delegate method to dismiss the modalViewController from the parent view when some data is passed.

My question is: how to dismiss the modalViewController from the parent view when no data is passed.

Thanks again!

Marcal
  • 1,371
  • 5
  • 19
  • 37

8 Answers8

13

You can call from the modalView :

[self dismissModalViewControllerAnimated:YES];

But... If you want to have a constant coding pattern, whatever the modal view "returns" something or not, I suggest you to dismiss you views from the parent (the one that calls, the one that dismiss). But you can do both.

Does that answer your question ?

Oliver
  • 23,072
  • 33
  • 138
  • 230
  • @Marcal : Uhhhh, there is no relation beetween data passed and dismissing a view... I don't understand your purpose. – Oliver Nov 11 '11 at 10:38
  • Well, I used the protocol-delegate method before to pass data to the parent view. When the parent view got the data, it dismissed the modal view. Basically my question is whether or not I can use this method even if I don't have to pass any data. – Marcal Nov 11 '11 at 15:29
  • Of course, just give nil as data and use an if-then statement in the protocol method to handle the case. Or if you have NEVER any data to pass, just build a delegate method that do not get data as param. – Oliver Nov 11 '11 at 19:39
  • remember what @chris said: `dismissModalViewController` is deprecated since IOS 6 – agmezr Dec 17 '14 at 20:39
8

Do take note that

[self dismissModalViewControllerAnimated:YES];

has been deprecated from iOS 6 onwards. Rather use

[self dismissViewControllerAnimated:YES completion:nil];
Chris
  • 4,593
  • 1
  • 33
  • 37
5

You can dismiss the modal view directly (i.e. not from the parent) using

[self dismissModalViewControllerAnimated:YES];
PengOne
  • 48,188
  • 17
  • 130
  • 149
4

For IOS6 and later use

[self dismissViewControllerAnimated:YES completion:nil];

Sinuhe Huidobro
  • 191
  • 2
  • 7
2

Swift 4 and newer:

self.dismiss(animated: true, completion: nil)
Cœur
  • 37,241
  • 25
  • 195
  • 267
bluefox
  • 136
  • 2
  • 16
0

Swift version...

self.dismissViewControllerAnimated(true, completion:nil)
JeremyWeir
  • 24,118
  • 10
  • 92
  • 107
0

You should use an unwind segue, especially since you can use it to get information from the presented view controller. This shows how to create one: http://spin.atomicobject.com/2014/10/25/ios-unwind-segues/

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
0

If you don't want to call it from self you can use a delegate to call it.

So you'll have a method viewControllerFinished or something that your view controller will call on it's delegate.

That way, whatever launches the modal view controller will also dismiss it. This is helpful if you need to get any information back from the view controller.

Randall
  • 14,691
  • 7
  • 40
  • 60