Skip to content

Instantly share code, notes, and snippets.

@laiso
Created January 5, 2012 13:34
Show Gist options
  • Save laiso/1565279 to your computer and use it in GitHub Desktop.
Save laiso/1565279 to your computer and use it in GitHub Desktop.
呼び出しが pushViewController でも presentModalViewController でも対応できるViewController 用のスニペット
@interface ChildController ()
@property(nonatomic, retain) UINavigationController* navigationController;
@end
@implementation ChildController
@synthesize navigationController;
- (void)viewDidLoad
{
[super viewDidLoad];
/**
* 呼び出しが pushViewController でも presentModalViewController でも対応できるViewController 用のスニペット
*/
if( !self.navigationController && ![self.parentViewController isKindOfClass:[UINavigationController class]] ){
/** 親元不明のModalViewController はNavigationBar と閉じるボタンをつけてやる */
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] init];
backButton.style = UIBarButtonItemStylePlain;
backButton.title = @"閉じる";
backButton.target = self;
backButton.action = @selector(dismissModalViewControllerAnimated:);
self.navigationItem.rightBarButtonItem = backButton;
self.navigationController.view = self.view;
[self.view addSubview:self.navigationController.navigationBar];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment