iOS - 通过代码加载导航控制器

发布于 2024-11-09 16:53:42 字数 249 浏览 2 评论 0原文

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

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

任何有关此的参考或代码片段都会有所帮助。 谢谢!

I have a navigation controller that was setup via the project wizard. Currently when the application is launched the navigation controller automatically gets created and displayed.

I now need to control the display of the navigation controller via code instead of via the .xib magic. How do I disable the automatic creation of the MainWindow.xib/RootViewController.xib? I confess I don't actually know what's going on and the relationship between MainWindow.xib and RootController.xib as the wizard set all this up.

Any references or code snippets on this would be helpful..
thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平生欢 2024-11-16 16:53:42

要创建不带笔尖的根导航控制器:

在您的应用程序委托中,您应该看到以下内容:

- (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 加载的导航控制器(该文件的名称在您的应用程序的 Info.plist 文件中指定;见下文)。

打开 MainWindow.xib 并断开应用程序委托的 navigationController 属性,然后删除对象选项板中的导航控制器(而不是窗口)对象。

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

将应用程序委托中的代码替换为以下内容:

- (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;
}

创建不带笔尖的主窗口:

您可能不需要这样做(并且我不建议这样做),但是因为你(有点)问...

删除MainWindow.xib。

在 main.m 中,将 UIApplicationMain 的最后一个参数替换为您的应用程序委托的名称(不带扩展名)。例如:

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

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

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

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

在您的应用程序委托中创建窗口:

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

    // The rest stays the same...
}

To create the root navigation controller without a nib:

In your App Delegate you should see the following:

- (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 refers to the navigation controller that was loaded from MainWindow.xib (the name of this file is specified in your app's Info.plist file; see below).

Open MainWindow.xib and disconnect the navigationController property of your App Delegate, then delete the Navigation Controller (not the Window) object in the Objects palette.

Remove the IBOutlet property from the navigationController @property declaration in your App Delegate's header file (since it will no longer be wired from a nib file).

Replace the code in your App Delegate with something along the following lines:

- (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;
}

To create the main window without a nib:

You probably don't need to do this (and I don't recommend it), but since you (sort of) asked...

Delete MainWindow.xib.

In main.m, replace the last argument to UIApplicationMain with the name of your App Delegate (with no extension). For instance:

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

Open your Info.plist file and delete the following two lines:

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

Remove the IBOutlet property from the window @property declaration in your App Delegate's header file.

Create the window in your App Delegate:

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

    // The rest stays the same...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文