我有一个要添加到导航栏的自定义按钮。所以这就是我到目前为止所拥有的RootViewController
(继承UIViewController
,UINavigationController
通过添加AppDelegate
):
在viewDidLoad
:
UIBarButtonItem *share = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon"] style:UIBarButtonItemStylePlain target:self action:@selector(share:)];
self.navigationController.navigationItem.rightBarButtonItem = share;
这是我UINavigationController
的设置方式:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar"] forBarMetrics:UIBarMetricsDefault];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
为什么这不起作用?
编辑:
在我替换self.navigationController.navigationItem.rightBarButtonItem
为self.navigationItem.rightBarButtonItem
. 这是为什么?我的 rootviewcontroller 是类型UIViewController
,我应该navigationItems
通过navigationController
. navigationController
在这种情况下是什么?