Home:ALL Converter>Dismissing a Presented View Controller

Dismissing a Presented View Controller

Ask Time:2013-02-01T07:14:21         Author:nikitahils

Json Formatter

I have a theoretic question. Now İ'm reading Apple's ViewController guide.

They wrote:

When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it. Although there are several techniques for notifying the presenting view controller that its presented view controller should be dismissed, the preferred technique is delegation.

But I can't explain, why I have to create a protocol in presented VC and add delegate varible, create delegate method in presenting VC for dismissing presented VC, instead of a simple call in presented view controller method

[self dismissViewControllerAnimated:NO completion:nil]?

Why is the first choice better? Why does Apple recommend it?

Author:nikitahils,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14636891/dismissing-a-presented-view-controller
Pranit :

Swift 3.0\n//Dismiss View Controller in swift \n\nself.navigationController?.popViewController(animated: true)\ndismiss(animated: true, completion: nil)\n",
2017-05-20T10:31:42
svena :

Quote from View Controller Programming Guide, \"How View Controllers Present Other View Controllers\".\n\n\n Each view controller in a chain of presented view controllers has\n pointers to the other objects surrounding it in the chain. In other\n words, a presented view controller that presents another view\n controller has valid objects in both its presentingViewController and\n presentedViewController properties. You can use these relationships to\n trace through the chain of view controllers as needed. For example, if\n the user cancels the current operation, you can remove all objects in\n the chain by dismissing the first presented view controller.\n Dismissing a view controller dismisses not only that view controller\n but also any view controllers it presented.\n\n\nSo on one hand it makes for a nice balanced design, good de-coupling, etc... But on the other hand it's very practical, because you can quickly get back to a certain point in navigation. \n\nAlthough, I personally would rather use unwinding segues than try to traverse backwards the presenting view controllers tree, which is what Apple talks about in this chapter where the quote is from. ",
2015-03-03T06:06:16
Abdurrahman Mubeen Ali :

One point is that this is a good coding approach. It satisfies many OOP principles, eg., SRP, Separation of concerns etc. \n\nSo, the view controller presenting the view should be the one dismissing it. \n\nLike, a real estate company who gives a house on rent should be the authority to take it back.",
2015-05-14T09:33:47
Mayur :

In addition to Michael Enriquez's answer, I can think of one other reason why this may be a good way to protect yourself from an undetermined state:\n\nSay ViewControllerA presents ViewControllerB modally. But, since you may not have written the code for ViewControllerA you aren't aware of the lifecycle of ViewControllerA. It may dismiss 5 seconds (say) after presenting your view controller, ViewControllerB.\n\nIn this case, if you were simply using dismissViewController from ViewControllerB to dismiss itself, you would end up in an undefined state--perhaps not a crash or a black screen but an undefined state from your point of view.\n\nIf, instead, you were using the delegate pattern, you would be aware of the state of ViewControllerB and you can program for a case like the one I described.",
2017-06-13T08:41:15
Rishi Chaurasia :

Swift\n\nlet rootViewController:UIViewController = (UIApplication.shared.keyWindow?.rootViewController)!\n\n if (rootViewController.presentedViewController != nil) {\n rootViewController.dismiss(animated: true, completion: {\n //completion block.\n })\n }\n",
2017-10-31T12:51:19
foundry :

I think Apple are covering their backs a little here for a potentially kludgy piece of API. \n\n [self dismissViewControllerAnimated:NO completion:nil]\n\n\nIs actually a bit of a fiddle. Although you can - legitimately - call this on the presented view controller, all it does is forward the message on to the presenting view controller. If you want to do anything over and above just dismissing the VC, you will need to know this, and you need to treat it much the same way as a delegate method - as that's pretty much what it is, a baked-in somewhat inflexible delegate method. \n\nPerhaps they've come across loads of bad code by people not really understanding how this is put together, hence their caution. \n\nBut of course, if all you need to do is dismiss the thing, go ahead.\n\nMy own approach is a compromise, at least it reminds me what is going on:\n\n [[self presentingViewController] dismissViewControllerAnimated:NO completion:nil]\n\n\n[Swift]\n\n self.presentingViewController?.dismiss(animated: false, completion:nil)\n",
2013-01-31T23:29:22
mbi :

I like this one:\n (viewController.navigationController?.presentingViewController\n ?? viewController.presentingViewController\n ?? viewController).dismiss(animated: true)\n",
2020-10-21T11:00:47
Suragch :

Updated for Swift 3\nI came here just wanting to dismiss the current (presented) View Controller. I'm making this answer for anyone coming here with the same purpose.\nNavigation Controller\nIf you are using a navigation controller, then it is quite easy.\nGo back to the previous view controller:\n// Swift\nself.navigationController?.popViewController(animated: true)\n\n// Objective-C\n[self.navigationController popViewControllerAnimated:YES];\n\nGo back to the root view controller:\n// Swift\nself.navigationController?.popToRootViewController(animated: true)\n\n// Objective-C\n[self.navigationController popToRootViewControllerAnimated:YES];\n\n(Thanks to this answer for the Objective-C.)\nModal View Controller\nWhen a View Controller is presented modally, you can dismiss it (from the second view controller) by calling\n// Swift\nself.dismiss(animated: true, completion: nil)\n\n// Objective-C\n[self dismissViewControllerAnimated:YES completion:nil];\n\nThe documentation says,\n\nThe presenting view controller is responsible for dismissing the view\ncontroller it presented. If you call this method on the presented view\ncontroller itself, UIKit asks the presenting view controller to handle\nthe dismissal.\n\nSo it works for the presented view controller to call it on itself. Here is a full example.\nDelegates\nThe OP's question was about the complexity of using delegates to dismiss a view.\n\nThis Objective-C answer goes into it quite a bit.\nHere is a Swift example.\n\nTo this point I have not needed to use delegates since I usually have a navigation controller or modal view controllers, but if I do need to use the delegate pattern in the future, I will add an update.",
2016-06-10T04:36:17
ErasmoOliveira :

If you are using modal use view dismiss.\n\n[self dismissViewControllerAnimated:NO completion:nil];\n",
2014-12-16T16:28:06
Michael Enriquez :

This is for view controller reusability.\n\nYour view controller shouldn't care if it is being presented as a modal, pushed on a navigation controller, or whatever. If your view controller dismisses itself, then you're assuming it is being presented modally. You won't be able to push that view controller onto a navigation controller.\n\nBy implementing a protocol, you let the parent view controller decide how it should be presented/pushed and dismissed/popped.",
2014-07-11T03:19:15
RegularExpression :

This is a lot of baloney. Delegation is fine when it is needed but if it makes the code more complex -- and it does -- then there needs to be a reason for it.\n\nI'm sure Apple has its reasons. But it is clearer and more concise to simply have the presented VC do the dismiss unless there is a true reason for doing otherwise and no one here as of today has presented one that I can see. \n\nProtocols are excellent when they're needed but object oriented design was never about having modules communicating unnecessarily with each other. \n\nTom Love (co-developer of Objective C) once commented that Objective C was \"elegant\", \"small\", \"crisp\" and \"well-defined\" (when comparing with C++). Easy for him to say. Delegation is a useful feature that seems to have been over-used \"just because\", and while I like working in the language, I dread the idea of felling compelled to use unnecessary syntax to make things more complex than they have to be.",
2016-02-05T02:56:53
Oshitha Wimalasuriya :

try this:\n\n[self dismissViewControllerAnimated:true completion:nil];\n",
2017-02-28T09:54:39
jhilgert00 :

In my experience, it comes in handy when you need to dismiss it from any ViewController you want and perform different tasks for each viewcontroller that dismisses it. Any viewController that adopts the protocol can dismiss the view in it's own way. (ipad vs iphone, or passing different data when dismissing from different views, calling different methods when dismissing, etc..)\n\nEdit:\n\nSo, to clarify, if all you ever want to do is dismiss the view, I see no need to setup the delegate protocol. If you need to do different things after you dismiss it from different presenting view controllers, It would be your best way to go using the delegate.",
2013-01-31T23:17:35
yy