如何在没有 xib 的情况下使用 UINavigationController?

发布于 2024-11-06 08:32:31 字数 621 浏览 0 评论 0原文

我对 Interface Builder 有一种非理性的厌恶。

到目前为止,我得到的只是黑屏。

- (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.
    PrayViewController* prayViewController = [[PrayViewController alloc] initWithTabBar];
    [_window addSubview: [[UINavigationController alloc] initWithRootViewController:prayViewController]];
    [prayViewController release];
    [self.window makeKeyAndVisible];
    return YES;
}

I have an irrational aversion to the Interface Builder.

Here's what I have so far, all I get is the black screen.

- (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.
    PrayViewController* prayViewController = [[PrayViewController alloc] initWithTabBar];
    [_window addSubview: [[UINavigationController alloc] initWithRootViewController:prayViewController]];
    [prayViewController release];
    [self.window makeKeyAndVisible];
    return YES;
}

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

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

发布评论

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

评论(2

叹倦 2024-11-13 08:32:31

您应该向窗口添加一个视图,如下所示:

UIViewController *rootController = [[MyRootViewController alloc] init];
navigationController = [[UINavigationController alloc]
                            initWithRootViewController:rootController];
[rootController release];

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// *** IMPORTANT DIFFERENCE:
[window addSubview:navigationController.view];

[window makeKeyAndVisible];

您将导航视图控制器的整个实例添加到窗口。

UIWindow 是 UIView 的后代,因此它只是继承 addSubview 方法。它需要另一个 UIView 作为参数。

You should add a view to the window, like this:

UIViewController *rootController = [[MyRootViewController alloc] init];
navigationController = [[UINavigationController alloc]
                            initWithRootViewController:rootController];
[rootController release];

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// *** IMPORTANT DIFFERENCE:
[window addSubview:navigationController.view];

[window makeKeyAndVisible];

You are adding the whole instance of the navigation view controller to the window.

UIWindow is a descendant of UIView so it just inherits the addSubview method. It expects another UIView as parameter.

拧巴小姐 2024-11-13 08:32:31

我会检查您的窗口及其视图是否通过 NSLog 返回您所期望的内容。如果没有看到创建窗口及其视图的代码,很难判断,但我怀疑其中之一没有正确创建。

I would check that your window and it view is returning what you expect via NSLog. Without seeing the code that creates the window and its view it is hard to tell but I suspect that one of those is not being created correctly.

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