简体   繁体   English

如何在导航 controller 上调用另一个 class 并将其添加到应用程序委托

[英]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.我创建了一个基于视图的应用程序,并在 appdelegate.h 文件中创建了 UINavigationcontroller object 并将其添加到 window 子视图中。 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.接下来在应用程序中完成启动我已经分配并初始化了一个 class,我希望在应用程序首次启动时查看它,顶部有导航栏。

So this is the code I have did to add the class to navigation bar所以这是我将 class 添加到导航栏的代码

- (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.但问题是我的测试 class 启动但它不显示导航栏。 What may be the problem?可能是什么问题?

You shouldn't add your own view controller's view as a subview of the navigation controller.您不应将自己的视图控制器的视图添加为导航 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.这样,您隐藏了导航控制器的视图(包括导航栏),它使导航 controller 毫无意义,因为它不知道您的视图 controller 想要成为其导航堆栈的一部分。

Instead, push it on the navigation controller's stack by using:相反,使用以下命令将其推送到导航控制器的堆栈上:

[navController pushViewController:myViewController animated:NO];

Use some like this (add these lines)使用一些这样的(添加这些行)

appDelegate.h appDelegate.h

UINavigationController *navController UINavigationController *navController

@property (nonatomic,retain) UINavigationController *navController;

and in.m和 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从其他角度来看,您需要制作 appDelegate class 的 object 然后访问该 ZA2F2AED4F8EBC2CBBDZC2 的导航 controller

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.这个概念可以帮助你。

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

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:其次,您不要将 viewController 添加到导航控制器视图,而是将视图 controller 推送到堆栈上,如下所示:

[navController pushViewController:t animated:NO];

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM