Rani
Rani

Reputation: 3453

How to call another class on navigation controller and add it to app delegate

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?

Upvotes: 2

Views: 2031

Answers (4)

Ishu
Ishu

Reputation: 12787

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.

Upvotes: 1

Jamie
Jamie

Reputation: 5112

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.

Upvotes: 0

omz
omz

Reputation: 53551

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];

Upvotes: 1

saadnib
saadnib

Reputation: 11145

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];

Upvotes: 0

Related Questions