如何添加一个右键到UINavigationController?

我正试图添加一个刷新button到导航控制器的顶部栏没有成功。

这是标题:

@interface PropertyViewController : UINavigationController { } 

这是我想如何添加它:

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)]; self.navigationItem.rightBarButtonItem = anotherButton; } return self; } 

尝试在viewDidLoad中做。 一般来说,你应该把任何东西都推迟到这个时候,无论如何,当一个UIViewController被调用的时候,它仍然可能在它显示之前还有一段时间,没有意义的提前做早期工作和占用内存。

 - (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)]; self.navigationItem.rightBarButtonItem = anotherButton; [anotherButton release]; } 

至于为什么它不是目前正在工作,我不能说100%的确定性没有看到更多的代码,但很多东西发生在init和视图加载之间,你可能会做一些导致导航项重置之间。

尝试将button添加到视图控制器的navigationItem中,该​​控件将被推送到您创build的PropertyViewController类上。

那是:

 MainViewController *vc = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; [infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside]; vc.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease]; PropertyViewController *navController = [[PropertyViewController alloc] initWithRootViewController:vc]; 

现在,这个以编程方式创build的infoButton将显示在导航栏中。 这个想法是,导航控制器从它即将显示的UIViewController中获取显示信息(标题,button等)。 你实际上并没有将button等直接添加到UINavigationController

似乎有些人(像我)可能会来这里寻找如何在Interface Builder中添加一个导航栏button。 下面的答案显示了如何做到这一点。

将导航控制器添加到您的故事板

select您的视图控制器,然后在Xcode菜单中select编辑器>embedded>导航控制器

在这里输入图像描述

或者,您可以从对象库中添加一个UINavigationBar

添加一个栏button项

UIBarButtonItem从对象库UIBarButtonItem顶部导航栏。

在这里输入图像描述

它应该是这样的:

在这里输入图像描述

设置属性

您可以双击“项目”将文本更改为“刷新”,但有一个实际的刷新图标,您可以使用。 只需selectUIBarButtonItem的属性检查器,然后select刷新

在这里输入图像描述

这会给你默认的刷新图标。

在这里输入图像描述

添加一个IB行动

控制从UIBarButtonItem拖动到视图控制器添加@IBAction

 class ViewController: UIViewController { @IBAction func refreshBarButtonItemTap(sender: UIBarButtonItem) { print("How refreshing!") } } 

而已。

有一个“刷新”的默认系统button:

 - (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *refreshButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshClicked:)] autorelease]; self.navigationItem.rightBarButtonItem = refreshButton; } - (IBAction)refreshClicked:(id)sender { } 

你可以使用这个:

 UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(method:)]; self.navigationItem.rightBarButtonItem = anotherButton; 
 -(void) viewWillAppear:(BOOL)animated { UIButton *btnRight = [UIButton buttonWithType:UIButtonTypeCustom]; [btnRight setFrame:CGRectMake(0, 0, 30, 44)]; [btnRight setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal]; [btnRight addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *barBtnRight = [[UIBarButtonItem alloc] initWithCustomView:btnRight]; [barBtnRight setTintColor:[UIColor whiteColor]]; [[[self tabBarController] navigationItem] setRightBarButtonItem:barBtnRight]; } 

这里是Swift的解决scheme(根据需要设置选项):

 var optionButton = UIBarButtonItem() optionButton.title = "Settings" //optionButton.action = something (put your action here) self.navigationItem.rightBarButtonItem = optionButton 

对于迅捷2:

 self.title = "Your Title" var homeButton : UIBarButtonItem = UIBarButtonItem(title: "LeftButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("yourMethod")) var logButton : UIBarButtonItem = UIBarButtonItem(title: "RigthButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("yourMethod")) self.navigationItem.leftBarButtonItem = homeButton self.navigationItem.rightBarButtonItem = logButton 

为什么你是UINavigationController子类? 如果您只需要添加一个button,就不需要inheritance它。

使用顶部的UINavigationController设置层次结构,然后在根视图控制器的viewDidLoad:方法中设置一个层次:设置button并通过调用将其附加到导航项目

 [[self navigationItem] setRightBarButtonItem:myBarButtonItem]; 

你可以使用self.navigationBar.topItem.rightBarButtonItem = anotherButton;

 UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 110, 50)]; view.backgroundColor = [UIColor clearColor]; UIButton *settingsButton = [UIButton buttonWithType:UIButtonTypeCustom]; [settingsButton setImage:[UIImage imageNamed:@"settings_icon_png.png"] forState:UIControlStateNormal]; [settingsButton addTarget:self action:@selector(logOutClicked) forControlEvents:UIControlEventTouchUpInside]; [settingsButton setFrame:CGRectMake(40,5,32,32)]; [view addSubview:settingsButton]; UIButton *filterButton = [UIButton buttonWithType:UIButtonTypeCustom]; [filterButton setImage:[UIImage imageNamed:@"filter.png"] forState:UIControlStateNormal]; [filterButton addTarget:self action:@selector(openActionSheet) forControlEvents:UIControlEventTouchUpInside]; [filterButton setFrame:CGRectMake(80,5,32,32)]; [view addSubview:filterButton]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view]; 

试试这个,它为我工作。

导航栏 ,并添加到右button的背景图像。

  UIBarButtonItem *Savebtn=[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"bt_save.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(SaveButtonClicked)]; self.navigationItem.rightBarButtonItem=Savebtn; 

使用此代码右键导航栏与您的标题,并单击右键后调用方法。

 UIBarButtonItem *btnSort=[[UIBarButtonItem alloc]initWithTitle:@"right" style:UIBarButtonItemStylePlain target:self action:@selector(sortedDataCalled)]; self.navigationItem.rightBarButtonItem=btnSort; } -(void)sortedDataCalled { NSLog(@"callBtn"); } 
  UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)]; self.navigationItem.rightBarButtonItem = rightBarButtonItem; 
 - (void)viewWillAppear:(BOOL)animated { [self setDetailViewNavigationBar]; } -(void)setDetailViewNavigationBar { self.navigationController.navigationBar.tintColor = [UIColor purpleColor]; [self setNavigationBarRightButton]; [self setNavigationBarBackButton]; } -(void)setNavigationBarBackButton// using custom button { UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@" Back " style:UIBarButtonItemStylePlain target:self action:@selector(onClickLeftButton:)]; self.navigationItem.leftBarButtonItem = leftButton; } - (void)onClickLeftButton:(id)sender { NSLog(@"onClickLeftButton"); } -(void)setNavigationBarRightButton { UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(onClickrighttButton:)]; self.navigationItem.rightBarButtonItem = anotherButton; } - (void)onClickrighttButton:(id)sender { NSLog(@"onClickrighttButton"); } 
  self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshData)]; } -(void)refreshData{ progressHud= [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; [progressHud setLabelText:@"拼命加载中..."]; [self loadNetwork]; } 

你应该添加你的barButtonItem在- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated方法。

只需复制并粘贴这个Objective-C代码。

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self addRightBarButtonItem]; } - (void) addRightBarButtonItem { UIButton *btnAddContact = [UIButton buttonWithType:UIButtonTypeContactAdd]; [btnAddContact addTarget:self action:@selector(addCustomerPressed:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:btnAddContact]; self.navigationItem.rightBarButtonItem = barButton; } #pragma mark - UIButton - (IBAction)addCustomerPressed:(id)sender { // Your right button pressed event } 

@Artilheiro:如果它是一个基于导航的项目,你可以创buildBaseViewController。 所有其他视图将inheritance这个BaseView。 在BaseView中,你可以定义generics方法来添加右键或改变左键文本。

例如:

@interface BaseController:UIViewController {

} – (void)setBackButtonCaption:(NSString *)caption;

(void)setRightButtonCaption:(NSString *)标题selectot:(SEL)select器;

@end //在BaseView.M中

(void)setBackButtonCaption:(NSString *)caption {

 UIBarButtonItem *backButton =[[UIBarButtonItem alloc] init]; backButton.title= caption; self.navigationItem.backBarButtonItem = backButton; [backButton release]; 

} – (void)setRightButtonCaption:(NSString *)caption selectot:(SEL)selector {

  UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init]; 
 rightButton.title = caption; rightButton.target= self; [rightButton setAction:selector]; self.navigationItem.rightBarButtonItem= rightButton; [rightButton release]; 

}

现在在任何自定义视图中,实现这个基础视图调用方法:

@interface LoginView:BaseController {

在某些方法中调用基本方法如下:

SEL sel = @selector(switchToForgotPIN);

[super setRightButtonCaption:@“忘记密码”selectot:sel];