gpt4 book ai didi

iOS - 通过代码加载导航 Controller

转载 作者:可可西里 更新时间:2023-11-01 05:24:48 25 4
gpt4 key购买 nike

我有一个通过项目向导设置的导航 Controller 。目前,当应用程序启动时,导航 Controller 会自动创建并显示。

我现在需要通过代码而不是通过 .xib 魔法来控制导航 Controller 的显示。如何禁用 MainWindow.xib/RootViewController.xib 的自动创建?我承认我实际上并不知道发生了什么以及 MainWindow.xib 和 RootController.xib 之间的关系,因为向导设置了所有这些。

关于此的任何引用或代码片段都会有所帮助。谢谢!

最佳答案

创建没有 nib 的根导航 Controller :

在您的 App Delegate 中,您应该看到以下内容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the navigation controller's view to the window and display.

self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}

self.navigationController 指的是从 MainWindow.xib 加载的导航 Controller (此文件的名称在您应用的 Info.plist 文件中指定;见下文)。

打开 MainWindow.xib 并断开 App Delegate 的 navigationController 属性,然后删除对象面板中的导航 Controller (不是窗口)对象。

从 App Delegate 的头文件中的 navigationController @property 声明中删除 IBOutlet 属性(因为它将不再从 nib 文件连接)。

将您的 App Delegate 中的代码替换为以下几行:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RootViewController *rootViewController = [[[RootViewController alloc] initWithNibName:nil bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];

self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}

创建没有 Nib 的主窗口:

你可能不需要这样做(我不推荐这样做),但既然你(有点)问...

删除 MainWindow.xib。

在 main.m 中,将 UIApplicationMain 的最后一个参数替换为您的 App Delegate 的名称(没有扩展名)。例如:

int retVal = UIApplicationMain(argc, argv, nil, @"TestProjectAppDelegate");

打开您的 Info.plist 文件并删除以下两行:

<key>NSMainNibFile</key>
<string>MainWindow</string>

从 App Delegate 的头文件中的 window @property 声明中删除 IBOutlet 属性。

在您的 App Delegate 中创建窗口:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// The rest stays the same...
}

关于iOS - 通过代码加载导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6119267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com