Confused
Confused

Reputation: 3926

How to add UINavigationBar in an UIViewController?

I have an UIViewController class(Say it is XXX). I present this view controller as modally by the code..

XXX *xxx = [ [XXX alloc] init];
[self presentModalViewController:xxx animated:YES];
[xxx release];

I want to add a navigation bar on the top of the XXX view. So I used UINavigationBar object in XXX's loadView method.

UINavigationBar *navBar = [ [UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:navBar];
[navBar release];

But, It throws an error as "EXC_BAD_ACCESS". Any help...?

Thanks

Upvotes: 5

Views: 18429

Answers (4)

Parth Bhatt
Parth Bhatt

Reputation: 19469

OPTION-1:

Try adding navigation bar from the XIB of viewController called XXX.

OPTION-2:

Add a UINavigationController and present it modally.

Replace your code :

XXX *xxx = [[XXX alloc] init];
[self presentModalViewController:xxx animated:YES];
[xxx release];

with this code:

XXX *xxx = [[XXX alloc] init];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:xxx];
[self presentModalViewController:navigation animated:YES];
[navigation release];

Hope this helps you.

Upvotes: 17

Rajesh
Rajesh

Reputation: 784

Replace your code with:

    XXX *xxx = [[ [XXX alloc] init]autorelease];
    [self presentModalViewController:xxx animated:YES];

    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:xxx];
    [self presentModalViewController:navigation animated:YES];
    [navigation release];

I think it will solve your "EXC_BAD_ACCESS" problem.

Upvotes: 2

iNeal
iNeal

Reputation: 1729

you can try this by adding toolbar at the top of the view. In many cases i have seen for poping MODAL controller this is nice solution. but if you want to navigate more controllers from MODAL controller then you should use UINavigationController.

enter image description here

Upvotes: 1

Dee
Dee

Reputation: 1897

you do it like this:

XXX *xxx = [[XXX alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc]  initWithRootViewController:xxx];
[self presentModalViewController:navigationController animated:YES];
[xxx release];
[navigationController release];

Upvotes: 0

Related Questions