Question

Just a question I've been pondering, 'traditionally' with modal viewcontrollers the presenting 'parent' view controller should dismiss the presented child.

Apple have stated that the presented child should not self dismiss, thus I tend to set up a delegate protocol just to dismiss a modal view controller.

This seems somewhat overkill,

I was wondering since objective C passes by reference anyway, and there wouldn't be a performance cost, couldn't I just pass a reference of the presenting parent viewcontroller to the modally presented child viewcontroller during instantiation and then make a method call back to the parent to dismiss the child?

Sorry if this is a stupid question...

Was it helpful?

Solution

The reason why you typically setup the delegate/protocol for something like this is that it makes your code much less coupled. Say you wanted to present your view as a popover or subview later? You may not think this is needed but it may as the project grows.

Since the parent controller handles its own modals, subviews, and popovers it knows what to do when a button to exit is pressed (or some other action). The modal (in this case) does not necessarily (and shouldn't!) know how it is being presented, hence it should tell its delegate that and let that controller handle it (popViewController, dismissViewController, removeSubview, etc...). This is a large part of understanding OOP and will help keep your code cleaner.

And no, this is not a stupid question in my opinion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top