# iOS4 alternatives for dismissViewControllerAnimated:completion:

My inspiration for creating this protocol came from iOS 5's "dismissViewControllerAnimated:completion:" addition to UIViewController. I wanted this functionality in iOS 4.3. I find using the modal view's -viewDidDissapear to invoke methods on the presenting view controller work's very well. One benefit is the ability to delay deallocation on the modal view controller. Please post any improvements I can make.

PresentorDelegateProtocol.h

@protocol PresentorDelegateProtocol <NSObject>
@optional

/*

Extra protocol methods defined in protocol for flexibility.
Main methods are:
- (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated;
- (void)modalViewDissapeared:(id)modalView;  //used in modal view's -viewDidDissapear

*/

- (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated;
- (void)modalViewDissapeared:(id)modalView;

// use the block in this method send messages to save state, etc.  This is the one I like to use.
- (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated withBlock:(void(^)())block;

// use in other classes that are not controlling dismissal of the modal view
- (void)executeBlockOnModalDissapearance: (void(^)())block;

@end


PresentingViewController.h

#import "PresentorDelegateProtocol.h"
@interface PresentingViewController : UIViewController <PresentorDelegateProtocol>
- (void)showModalVC;
@end


ModalViewController.h

#import "PresentorDelegateProtocol.h"
@interface ModalViewController : UIViewController
@property (nonatomic, assign) id <PresentorDelegateProtocol> presentorDelegate;
- (void)close;
@end


PresentingViewController.m

#import "PresentingViewController.h"
#import "ModalViewController.h"
@implementation PresentingModalViewController
- (void)showModalVC
{
ModalViewController *modalVC = [[ModalViewController alloc] initWithNibName:@"ModalViewController" bundle:nil];
modalVC.presentorDelegate = self;
[self presentModalViewController:modalVC animated:YES];
}
- (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated
{
if ([modalView isKindOfClass:[ModalViewController class]]) {
NSLog(@"Can invoke based on class");
}
[self dismissModalViewControllerAnimated:animated];
}
- (void)dismissPresentingModalViewController:(id)modalView animated:(BOOL)animated withBlock:(void(^)())block
{
block();
/* execute block before or after calling to dismiss modal view */
[self dismissPresentingModalViewController:modalView animated:animated];
//block();
}
- (void)modalViewDissapeared:(id)modalView
{
if ([modalView isKindOfClass:[ModalViewController class]]) {
NSLog(@"Do stuff based on class.");
}
}
- (void)executeBlockOnModalDissapearance: (void(^)())block
{
block();
NSLog(@"This delay's dealloc on modal view until block completes");
}
@end


ModalViewController.m

#import "ModalViewController.h"
@implementation ModalViewController
@synthesize presentorDelegate;

- (void)close
{
if (1 == 0 /*need to do something before dealloc*/){
[self.presentorDelegate dismissPresentingModalViewController:self animated:YES withBlock:^{
NSLog(@"Do stuff with block.  Save, animate, etc");
}];

} else {
[self.presentorDelegate dismissPresentingModalViewController:self animated:YES];
}
}

- (void)viewDidDisappear:(BOOL)animated
{
if (1 == 0 /*stuff to do*/){
[self.presentorDelegate executeBlockOnModalDissapearance:^{
// do stuff before modal view is deallocated
}];
}
[self.presentorDelegate modalViewDissapeared:self];

presentorDelegate = nil;
[super viewDidDisappear:animated];
}
@end;

-

Two light suggestions:

• disappear has one s and two ps.
• the word "block" to describe a block is accurate but not very descriptive. OK, so I need to pass a block, but I can see that from the parameter list. What the block is for should be explained in the method name: this is why Apple often uses phrases like "completion handler" or "comparator" to describe block parameters.
-

It's not very clear from your description of which problem you're trying to solve and how. The role of the objects is not really clear. Please try to describe the proposed architecture so that it's becomes easier to understand.

how the objects react to events and interact with each other.

• Is one or both of the classes supposed to be reused or are you describing a pattern that has to reimplemented for each usage?
• Which events on the view controllers lead to execution of which methods?
• How do the objects interact?

Glimpsing over the code I noticed the way you use blocks. I don't see the benefit of blocks when the method that takes the block just immediately executes it. Are the blocks supposed to be kept around and called later?

-