Marcal
Marcal

Reputation: 1371

Dismiss Modal View

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!

Upvotes: 4

Views: 10918

Answers (8)

bluefox
bluefox

Reputation: 166

Swift 4 and newer:

self.dismiss(animated: true, completion: nil)

Upvotes: 2

ABakerSmith
ABakerSmith

Reputation: 22979

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/

Upvotes: 0

JeremyWeir
JeremyWeir

Reputation: 24388

Swift version...

self.dismissViewControllerAnimated(true, completion:nil)

Upvotes: 0

Chris
Chris

Reputation: 4593

Do take note that

[self dismissModalViewControllerAnimated:YES];

has been deprecated from iOS 6 onwards. Rather use

[self dismissViewControllerAnimated:YES completion:nil];

Upvotes: 8

Sinuhe Huidobro
Sinuhe Huidobro

Reputation: 191

For IOS6 and later use

[self dismissViewControllerAnimated:YES completion:nil];

Upvotes: 4

Oliver
Oliver

Reputation: 23550

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 ?

Upvotes: 13

Randall
Randall

Reputation: 14849

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.

Upvotes: 0

PengOne
PengOne

Reputation: 48406

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

[self dismissModalViewControllerAnimated:YES];

Upvotes: 5

Related Questions