[[self navigationController] pushViewController:viewController animated:YES];
instead call:
MyNavigationController *presentModal = [[[MyNavigationController alloc] initWithRootViewController:viewController] autorelease];
[presentModal setDismisser:self];
UIBarButtonItem *cancelButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(doDismiss)] autorelease];
[[viewController navigationItem] setLeftBarButtonItem:cancelButton];
[[self navigationController] presentModalViewController:presentModal animated:YES];
where doDismiss is:
- (void)doDismiss {
[self dismissModalViewControllerAnimated:YES];
}
that handles showing and canceling.
For auto-dismiss, we catch the pop that the OAuth2ViewController will do:
@interface MyNavigationController : UINavigationController
@property (nonatomic, assign) UIViewController *dismisser;
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
@end
@implementation MyNavigationController
@synthesize dismisser;
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
if ([[self viewControllers] count] <= 1) {
UIViewController *result = [[[self topViewController] retain] autorelease];
[dismisser dismissModalViewControllerAnimated:YES];
return result;
} else {
return [super popViewControllerAnimated:animated];
}
}
@end
Warning: I didn't actually try the auto-dismiss case.