Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need to remove toView from container view after successful dismissal transition?

I'm reading View Controller Programming Guide for iOS these days, it says:

Because transitions can be canceled, you should use the return value of the transitionWasCancelled method of the context object to determine what cleanup is required. When a presentation is canceled, your animator must undo any modifications it made to the view hierarchy. A successful dismissal requires similar actions.

in Presentations and Transitions->Customizing the Transition Animations->Implementing Your Animator Objects->Cleaning Up After the Animations.

And sample code provided by Apple:

BOOL success = ![transitionContext transitionWasCancelled];

// After a failed presentation or successful dismissal, remove the view.
if ((self.presenting && !success) || (!self.presenting && success)) {
     [toView removeFromSuperview];
}

// Notify UIKit that the transition has finished
[transitionContext completeTransition:success];

I know that in dismissal, we first need to add toView to container view, but why we need to remove toView after successful dismissal? I have tested that if I don't call removeFromSuperview after successful dismissal, everything works fine.

And what will happen if the dismissal failed? It will still show the presented view controller or dismiss it and show the presenting view controller?

Anyone know about this? Thanks a lot.

like image 789
Samuel Avatar asked Nov 08 '22 17:11

Samuel


1 Answers

Usually this will happen when you're using custom animations with interactions. But if in case animation gets cancelled and if you do not handle cancel state then, it will remain in presented view controller state.(in your case)

like image 109
Siddhesh Mahadeshwar Avatar answered Nov 15 '22 05:11

Siddhesh Mahadeshwar