How can I open the modal view and the previous view of the navigation controller at the same time?

I want to pop the modal look and the previous look at the same time. For example, look at the calendars app. When I am on the Edit screen and select Delete Event , I return to the calendar view.

The Edit screen appears, which was presented modally, as well as the Event screen (where the user simply views the calendar event). My problem is that I know how to get a modal view, but from the same subclass of UIViewController (the Edit screen in this example).

How can I get a view that is not modal?

I thought about displaying the modal view, as you usually do, then place the NSNotification in a subclass of the Event screen (for example) of the UIViewController and tell it to also display that view.

Another thing is that for animation this should be an dismissModalViewControllerAnimated animation (slide down), and not a popViewControllerAnimated animation (slide to the left).

Looking for a better solution, I found this one that does not work in my case (at least with popViewControllerAnimated ).

+2
source share
1 answer

You need to use the delegate template to notify the modal “parent” so that it rejects the modal view controller (animated: NO) and pops out of the stack (animated: YES).

This is exactly what happens in the Calendar application - just pay attention to what happens with the title of the navigation bar when you confirm the deletion of the event. You can see the name that quickly changes from “Change” to “Event Details” as this type is unloaded from the navigation stack.

So, if we're talking about a calendar application, create a protocol in your modular view controller using a method like didConfirmEventDeletion :

 @protocol ModalViewDelegate <NSObject> - (void)didConfirmEventDeletion; @end @interface ModalViewController... @property (nonatomic, assign) id<ModalViewDelegate> delegate; @end 

And implementation:

 @implementation ModalViewController - (void)deleteEventMethod { ... if ([self.delegate respondsToSelector:@selector(didConfirmEventDeletion)]) [self.delegate didConfirmEventDeletion]; } 

Then, in the controller of the parent view, declare yourself a delegate for the modal and implement didConfirmEventDeletion :

 - (void)didConfirmEventDeletion { [self dismissModalViewControllerAnimated:NO]; [self.navigationController popViewControllerAnimated:YES]; } 

PS: there may be several typos since I wrote this code without memory ...

+10
source

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


All Articles