我在显示导航栏的 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/