Reject ModalViewController from another modalViewController

So I have a viewController A that represents a modal viewController B.

Then B represents viewController C.

What I'm trying to do is reject viewController B, since I no longer need it. Is there an easy way to reject B and keep C on the screen as child A, maybe?

+1
source share
4 answers

Obviously, as many have said, this in the world of textbooks would / should be a delegate problem. Where A represents B and B represents C Then, when C is executed, it tells B which rejects C , and then B tells A reject B

I would say that you essentially create a set of modally represented view controllers that make up the navigation stack. I would most likely implement it as such. Where A will be the root view controller. A then pushes B onto the stack, and B will push C onto the stack. When C was executed, it could just popToRootViewControllerAnimated: or popToViewController:A animated:YES if A not rootViewController.

Also, removing B from under C seems problematic. But this does not sound as shown on the stone based on your comment:

But in any case, this is not the end of the world if I keep this viewController there for a while. -

This will at least set a fairly clean delegate setting.

It still seems that you basically know that when the user has finished using C , they will no longer need B If so, you can blindly fire two or more dispatchers at once. And the code for two is pretty simple at the same time. (assuming iOS version> 5.0)

 [self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES]; 

Please note that I said blindly! This code does not forgive and makes assumptions. For example, it is most obvious that the view manager actually has a presentingViewController and that this view controller has a presentingViewController . If any of these criteria is not met, this code will be nothing . This can easily happen if you restructure your application at some point.

So, if you decide to use this line of code, use it very carefully. And please consider using the UINavigationController for this view hierarchy, or at least for delegation.

+2
source

You may be able to do this work:

  • provide property B as "dieOnDismiss"

  • implement 'rejectViewControllerAnimated: YES' in B and forward the message super

  • C optionally sets this property

  • when C sends B 'rejectViewControllerAnimated:', B sends the dismissal to super, immediately sends A 'rejectViewControllerAnimated: NO'

Not sure if you can use animated ones in one transition and look like this: you can do it. But if not, you can just go back to A without animation.

+1
source

You can try to make A conform to a protocol that defines how C is presented, then BB calls A to initiate the presentation of C, and not B, responsible for representing C itself. If C can exist independently of B, then A effectively acts as " parent "for both.

+1
source

You may have a share instance that contains a weak pointer to your root view controller (so A here). Use this instance instance to represent / fire C whenever you want from A using this pointer to A.

EDIT

This will follow the response from TimD. In any case, in the proposed approach, C and B will both be represented from A.

0
source

Source: https://habr.com/ru/post/1390588/


All Articles