[翻译]  rightBarButtonItem does not appear in Navigation Bar iOS

[CHINESE]  rightBarButtonItem不会出现在导航栏iOS中


I'm having problems displaying the rightBarButtonItem of the Navigation Bar - I'm attempting to create it programmatically in the Application Delegate, where my UINavigationController is set up.

我在显示导航栏的rightBarButtonItem时遇到问题 - 我正在尝试以编程方式在Application Delegate中创建它,我的UINavigationController已设置在其中。

Code is as follows:

代码如下:

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

// Override point for customization after application launch.
RSCListViewController *list = [[RSCListViewController alloc] initWithStyle:UITableViewStylePlain];
self.navController = [[UINavigationController alloc] initWithRootViewController:list];

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"+"
                                                              style:UIBarButtonItemStylePlain
                                                             target:list
                                                             action:@selector(addPressed:)];

self.navController.navigationItem.rightBarButtonItem = barButton;

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

[DatabaseManager openDatabase];

return YES;
}

Running the application, no button item appears on the navigation bar.

运行应用程序时,导航栏上不会显示任何按钮项。

I'm not sure whether I have missed something obvious - my attempts to rectify the problem using related Stack Overflow threads haven't yielded any success.

我不确定我是否遗漏了一些明显的东西 - 我尝试使用相关的Stack Overflow线程纠正问题并没有取得任何成功。

Any help appreciated.

任何帮助赞赏。

1 个解决方案

#1


31  

You need to attach your bar button item to your custom view controller, not to the navigation controller. From Updating the Navigation Bar:

您需要将条形按钮项目附加到自定义视图控制器,而不是导航控制器。从更新导航栏:

In addition, the navigation controller object builds the contents of the navigation bar dynamically using the navigation items (instances of the UINavigationItem class) associated with the view controllers on the navigation stack. To change the contents of the navigation bar, you must therefore configure the navigation items for your custom view controllers.

此外,导航控制器对象使用与导航堆栈上的视图控制器相关联的导航项(UINavigationItem类的实例)动态地构建导航栏的内容。要更改导航栏的内容,必须配置自定义视图控制器的导航项。

(...)

(......)

The navigation controller updates the right side of the navigation bar as follows:

导航控制器更新导航栏的右侧,如下所示:

  • If the new top-level view controller has a custom right bar button item, that item is displayed. To specify a custom right bar button item, set the rightBarButtonItem property of the view controller’s navigation item.

    如果新的顶级视图控制器具有自定义右侧栏按钮项,则显示该项。要指定自定义右侧栏按钮项,请设置视图控制器导航项的rightBarButtonItem属性。

  • If no custom right bar button item is specified, the navigation bar displays nothing on the right side of the bar.

    如果未指定自定义右侧栏按钮项,则导航栏将在栏的右侧不显示任何内容。

Therefore, replace:

因此,替换:

self.navController.navigationItem.rightBarButtonItem = barButton;

with:

有:

list.navigationItem.rightBarButtonItem = barButton;

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号