0

I have a view controller and I want to implement a navigation controller inside it, but when I implemented it through Interface Builder or programmatically it doesn't work.

Most of it I implemented through Interface Builder, but here is the code I implemented in the AppDelegate, and which I am trying to implement in my view controller.

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[window addSubview:navController.view];

I know I can't implement "window" in a view controller so I tried this in my view controller:

[self.view addSubview:navController];

But it still doesn't work.

2
  • you need to do it like this [self.window addSubview:navController.view] and before initWithRootViewController you need to alloc your viewController object Commented Jul 26, 2011 at 9:10
  • Chk my answer. And do it the same way Commented Jul 26, 2011 at 9:11

2 Answers 2

2

In the app delegate's applicationDidFinishLaunching you have to add this code.

viewController=[[myViewController alloc]init];
navigationController=[[UINavigationController alloc]initWithRootViewController:viewController];
[self.window addSubview:navigationController.view];

where viewController is the object of the myViewController class to which u need to add the navigation controller. It s declared in the header file of appDelegate.Similarly the navigationController is also declared in tat. Hope this helps.

New code:

AppSettings *settings = [[AppSettings alloc] init];
UINavigationController *navCont = [[UINavigationController alloc] initWithRootViewController:settings];
navCont.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController presentModalViewController:navCont animated:YES];
[settings release];
[navCont release];
Sign up to request clarification or add additional context in comments.

11 Comments

I have tried implementing this in my appDelegate under applicationDidFinishLaunching but I want to implement it through my own viewController.
then try replacing the viewcontroller with self and add this code in the loadview method of the the viewcontroller in which u want to implement....
Like this: -(void)loadView { viewController=[[NewAlarm alloc]init]; navController =[[UINavigationController alloc] initWithRootViewController:self]; [viewController addSubview:navController]; }
b4 tat i need to kno sometin....u want to move from ur current view to NEwALarm view ? do u have a nav controller in ur current view controller ?
I have my original view, and from that I launch the NewAlarm view but I don't do that using the navigation controller. I use presentModalViewController instead.
|
0

Try this in your -applicationDidFinishLaunching:

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.