BarButtonItem 不显示

发布于 2024-12-01 05:05:52 字数 1040 浏览 3 评论 0原文

我创建了一个新的基于窗口的应用程序,以及一个子类 UIViewController 的 RootViewController。我在 AppDelegate 中创建了一个 UINavigationController,当我将 UINavigationController 的视图添加到窗口时,我确实看到了顶部的导航栏。但是,我似乎无法向导航栏添加按钮 - 没有编译错误,但按钮(和标题)没有显示。代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    RootViewController * rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

    UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:nil action:nil];
    navigationController.title = @"Foo";

    navigationController.navigationItem.rightBarButtonItem = next;

    [self.window addSubview:navigationController.view];

    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];
    return YES;


}

我做错了什么?

I created a new window based application, and a RootViewController that sublcasses UIViewController. I created a UINavigationController in the AppDelegate and when I add the UINavigationController's view to the window, I do see the navigation bar on top. However, I cannot seem to add a button to the navigation bar - there are no compile errors, but the button (and the title) does not show up. The code is below:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    RootViewController * rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

    UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:nil action:nil];
    navigationController.title = @"Foo";

    navigationController.navigationItem.rightBarButtonItem = next;

    [self.window addSubview:navigationController.view];

    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];
    return YES;


}

What am I doing wrong?

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

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

发布评论

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

评论(2

枯寂 2024-12-08 05:05:52

将此:更改

navigationController.navigationItem.rightBarButtonItem = next;

为:

rootViewController.navigationItem.rightBarButtonItem = next;

Change this:

navigationController.navigationItem.rightBarButtonItem = next;

to this:

rootViewController.navigationItem.rightBarButtonItem = next;
梦一生花开无言 2024-12-08 05:05:52

可能 style:UIBarButtonItemStyleDone 仅在编辑模式下使用为 YES。如果在根控制器或子视图控制器中,您可以尝试调用 self.editing = YES,但在 AppDelegate 中则不能。

May be style:UIBarButtonItemStyleDone only used in editing mode is YES. If in Root controller or sub view controller, you may try call self.editing = YES, but in AppDelegate you can't.

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