Advertisements

UINavigationController as a Modal View


On my current iPhone project I need to display an Options view that will behave as a navigation controller with a main table view and associated subviews. I was able to accomplish this using various sources out there but feel the need to compile it all in one place.

For the purposes of this example I have a main view controller and a secondary view controller. The main view controller is the primary view controller for the application and the secondary view controller will be the root view controller of the navigation controller. Let’s also assume that I have a NIB file for the secondary view controller. In my application I wish to show a UINavigationController modally as the help section.

Changes to MainViewController.h:

#import "SecondaryViewController.h"

@class SecondaryViewController;

@interface MainViewController : UIViewController { SecondaryViewController *secondaryViewController; UINavigationController *navController;}

- (IBAction)navigateToSecondaryViewController;

@end

All I’ve done here is tell the main view controller where to find the secondary view controller and then assign it as a delegate to the secondaryViewDelegate protocol which will be defined in SecondaryViewController.h. I also created a quick IBAction method to tie to any button on the view.

Changes to MainViewController.m:

(IBAction) navigateToSecondaryViewController { if (secondaryViewController == nil) {  informationTableViewController = [[SecondaryViewController alloc]                    initWithNibName:@"SecondaryViewController"                     bundle:[NSBundle mainBundle]];  secondaryViewController.delegate = self; }

 if (navController == nil) {  navController = [[UINavigationController alloc]        initWithRootViewController:secondaryViewController]; }

 [self presentModalViewController:navController animated:YES];

}

- (void)closeSecondaryViewController { [self dismissModalViewControllerAnimated:YES];}

- (void)dealloc { if (secondaryViewController != nil) {  [secondaryViewController release]; }  if (navController != nil) {  [navController release]; }        [super dealloc];}

In my navigateToSecondaryViewController method I now initialize my view controllers if necessary. Assign the secondary view controller (which contains the information I want to display) to the navigation controller and display it modally. The closeSecondaryViewController method is the delegate method from the SecondaryViewController class which we will define next.

 

SecondaryViewController.h:
#import 

@protocol SecondaryViewDelegate 

- (void)closeSecondaryViewController;

@end

@interface SecondaryViewController : UIViewController { id delegate;}

@property (assign) id delegate;

@end/uikit.h>/uikit.h>/uikit.h>/uikit.h>
Define the header for the secondary view controller, define a protocol with a single method and create a delegate property for calling the delegate method. 

SecondaryViewController.m

#import “SecondaryViewController.h”

@implementation SecondaryViewController

@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {  self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;    }    return self;}

- (void)viewDidLoad {    [super viewDidLoad];

 UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] init]; closeButton.title = @"Done"; closeButton.target = self; closeButton.style = UIBarButtonItemStyleDone; [closeButton setAction:@selector(closeButtonTouch)];

 self.navigationItem.title = @"Options";

 self.navigationItem.rightBarButtonItem = closeButton; [closeButton release];}

- (void)closeButtonTouch { [delegate closeSecondaryViewController];}

- (void)dealloc {    [super dealloc];}

@end
 
In the initWithNibName method I've just modified the animation style for displaying this view modally. The default is to slide in from the bottom of the screen. In the viewDidLoad method I just create a button to close the view, add it to the navigation item for the view controller and assign it a selector method. The selector method calls the delegate method that will close the view.

 

Advertisements

About Ruprect
iOS, Android and web developer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: