如何在NavigationBar中添加按钮

问题描述

我想添加这样的按钮以分享tweet:image

这就是我想打电话的内容

[[UIApplication sharedApplication] canopenURL:[NSURL URLWithString:@"twitter://"]];

解决方法

您可以像这样向UINavigationItem添加按钮:

- (void)loadView {
    [super loadView];
    
    UIImage *hearthImage = [UIImage imageNamed:@"heart" inBundle:[NSBundle bundleForClass:[self class]]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:hearthImage style:UIBarButtonItemStylePlain target:self action:@selector(tapButton:)];
}

- (void) tapButton: (id) sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://twitter.com/intent/tweet?text=YOUR_TEXT_HERE"]]];
}