我是一名有用的助手,可以为您翻译文本。
我有一个自定义按钮,想要添加到导航栏上。所以这是我在我的
在
我有一个自定义按钮,想要添加到导航栏上。所以这是我在我的
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
,它就起作用了。为什么呢?我的根视图控制器是UIViewController
类型的,所以我应该通过navigationController
访问navigationItems
。在这种情况下,navigationController
是什么?
UIViewController
都包含一个对其导航控制器的引用,使用属性navigationController
。但是,navigationItem
是UIViewController
的属性,而不是UINavigationController
的属性(UINavigationController
继承它,因为它是UIViewController
的子类)。我想这可能是一个常见的误解,即navigationItem
是UINavigationController
的属性。还有一个问题,如果视图控制器没有导航栏,将其作为UINavigationController
的属性是否更合理? - darkskynavigationItem
是视图控制器的属性?视图控制器和navigationItem
之间的关系是什么?一个弱指针? - darksky