BarButtonItem 不显示
我创建了一个新的基于窗口的应用程序,以及一个子类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此:更改
为:
Change this:
to this:
可能 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.