首页 > 解决方案 > 如何将按钮添加到导航栏

问题描述

我想添加这样的按钮来分享推文:图片

这是我想调用的:

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

标签: iosobjective-ctheos

解决方案


您可以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"]]];
}

推荐阅读