objective-c - rightBarButtonItem 没有出现在 Navigation Bar iOS 中

标签 objective-c uinavigationcontroller uibarbuttonitem uinavigationitem rightbarbuttonitem

我在显示导航栏的 rightBarButtonItem 时遇到问题 - 我试图在应用程序委托(delegate)中以编程方式创建它,我的 UINavigationController 就是在其中设置的。

代码如下:

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

运行应用程序,导航栏上没有出现按钮项。

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

感谢任何帮助。

最佳答案

您需要将您的条形按钮项目附加到您的自定义 View Controller ,而不是导航 Controller 。来自 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.

(...)

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.

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

因此,替换:

self.navController.navigationItem.rightBarButtonItem = barButton;

与:

list.navigationItem.rightBarButtonItem = barButton;

关于objective-c - rightBarButtonItem 没有出现在 Navigation Bar iOS 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12395687/

相关文章:

iOS 控制中心触摸传递到我的 ScrollView ?

objective-c - 使用日历存储。

ios - 无法使用默认的 UINavigationController 后退按钮

iphone - iOS:如何在没有任何可见用户界面的情况下正确使用导航 Controller

ios - 使自定义导航按钮在触摸时发光?

ios - 更改后退导航栏按钮的字体

c++ - Objective-C 是否存在 C+ +'s "const 方法的近似值?

objective-c - indexOfObjectIdenticalTo : returns value outside of array count

iOS:如果不使用 UINavigationController,以编程方式查看导航堆栈中的 View Controller ?

ios - 在 View Controller 上添加导航栏