2

I have created a view based application and in the appdelegate .h file I have created UINavigationcontroller object and added it to window subview. Next in the app did finish launching I had allocated and initialised a class that I want to be viewed when the app first launches with the navigation bar on the top of it.

So this is the code I have did to add the class to navigation bar

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [window addSubview:navController.view];
    //test is the class that i want to be viewed first when  the app launches first
    test *t = [[test alloc]initWithNibName:@"test" bundle:nil];
    [navController.view addSubview:t.view];
    [window makeKeyAndVisible];

    return YES;
}

but the problem is my test class launches but it does not display the navigation bar. What may be the problem?

halfer
  • 19,824
  • 17
  • 99
  • 186
Rani
  • 3,333
  • 13
  • 48
  • 89

4 Answers4

1

You shouldn't add your own view controller's view as a subview of the navigation controller. This way, you hide the navigation controller's view (including the navigation bar) and it makes the navigation controller pointless, because it doesn't know that your view controller wants to be part of its navigation stack.

Instead, push it on the navigation controller's stack by using:

[navController pushViewController:myViewController animated:NO];
omz
  • 53,243
  • 5
  • 129
  • 141
1

Use some like this (add these lines)

appDelegate.h

UINavigationController *navController

@property (nonatomic,retain) UINavigationController *navController;

and in .m

test *t = [[test alloc]initWithNibName:@"test" bundle:nil]; 
self.navController=[[[UINavigationController alloc] initWithRootViewController:t] autorelease];

[window addSubview:self.navController.view];
 [window makeKeyAndVisible];

it helps you.

From other view you need to make object of the appDelegate class then access the navigation controller of that class

see this

yourAppDelegateClass *objAppDelegate=(yourAppDelegateClass *)[[UIApplication sharedApplication] delegate];

now for pushing a view

SecondView *s=[[[SecondView alloc] initWithNibName:@"yournibName" bundle:nil] autorelease];
[objAppDelegate.navController pushViewController:s animated:YES];

this concept help you.

Ishu
  • 12,797
  • 5
  • 35
  • 51
  • hi ishu, my code is working properly for the delegate class but what if i want to call the navigation bar on another controller .Thanks – Rani Apr 28 '11 at 08:03
  • my application is for view based,so if i create a window based application and use same code it will work na – Rani Apr 28 '11 at 11:15
0

Navigation bar is not coming because you are setting navigation controller's view to your test view. You can do it by setting navigation controller's view controllers which requires an array as -

test *t = [[test alloc]initWithNibName:@"test" bundle:nil];
NSArray *viewControllers = [[NSArray alloc] initWithObjects:t,nil];
[self.navigationController setViewControllers:viewControllers];
saadnib
  • 11,145
  • 2
  • 33
  • 54
0

First, I don't see where you alloc init your navController. Secondly, you don't add a viewController to a navigation controllers view, rather you push the view controller onto the stack, like this:

[navController pushViewController:t animated:NO];

Hope this helps.

Jamie
  • 5,090
  • 28
  • 26