2

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

The Edit screen, which was presented modally is popped as well as the the Event screen (where the user is just viewing the calendar event). The problem I am having is that I know how to pop a modal view, but from the same UIViewController subclass (Edit screen in this example).

How can I pop a view that isn't modal?

I was thinking about popping the modal view as you would normally, then posting a NSNotification to the 'Event' (for instance) screen's UIViewController subclass and telling it to pop that view as well.

The other thing is that for the animation, it should be the dismissModalViewControllerAnimated animation (slide down) and not the popViewControllerAnimated animation (slide left).

Looking for a better solution, I found this, which doesn't work in my case (at least not with popViewControllerAnimated.

double-beep
  • 5,031
  • 17
  • 33
  • 41
elusive
  • 460
  • 5
  • 24
  • UPDATE: sending the `NSNotification` doesn't work either. To further dissect the issue, I grabbed the array of view controllers on the '`View`' screen like so: `NSLog(@"received notification");` `[self.navigationController popViewControllerAnimated:YES];` `NSArray *controllers = [self.navigationController viewControllers];` `for (UIViewController *c in controllers) {` ` NSLog(@"%@",[c title]);` `}` – elusive Nov 16 '11 at 03:07

1 Answers1

10

You need to use the delegate pattern to notify the modal "parent" that it should dismiss the modal view controller (animated:NO) and pop itself off the stack (animated:YES).

This is exactly what happens on the Calendar App - just pay attention to what happens to the navigation bar title when you confirm an event deletion - you can see the title quickly changing from "Edit" to "Event Details" as that view is being popped out off the navigation stack.

So in a nutshell, if we were talking about the calendar app, in your modal view controller, create a protocol with 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 your parent view controller, declare itself as the delegate for the modal and implement didConfirmEventDeletion:

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

PS: there might be a few typos as I wrote this code off memory...

Rog
  • 18,602
  • 6
  • 76
  • 97
  • Thanks for that. It works after I finally deleted a property called navigationController which is passed from the previous view when you call [self.navigationController pushViewController:someView...]; Having this property made it so that the navigation controller was nil as soon as it reached the 'Event' screen. Therefore it couldn't be popped of the navigation controller stack. However, I think we have a different idea of what the view transition should be. When I delete a (calendar) event, the animation is slide down. When i do it in my project, it is slide right. – elusive Nov 16 '11 at 22:51
  • If I change reverse the animations; YES on modal and NO on pop (or do any other combination of YES/NO on modal/nav animations), the app crashes... :/ Probably because the second view has to be the one with an animation. I'm just guessing now D: – elusive Nov 16 '11 at 22:54
  • 1
    `- (void)dismissModalViewControllerAnimated:(BOOL)animated` is deprecated. Documentation says `Use dismissViewControllerAnimated:completion: instead.` – Dan2552 Sep 23 '12 at 23:48