如何向 UINavigationController 添加右键?

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

下面是标题:

@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;
}
219125 次浏览

试试在 viewDidLoad 中做。一般来说,您应该将任何可能的事情推迟到那个时候,当 UIViewController 被启动时,它可能还需要一段时间才会显示,没有必要提前做工作并占用内存。

- (void)viewDidLoad {
[super viewDidLoad];


UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
// exclude the following in ARC projects...
[anotherButton release];
}

至于为什么它现在不能工作,我不能100% 肯定地说没有看到更多的代码,但很多事情发生在 init 和视图加载之间,你可能正在做一些事情,导致导航项重置之间。

为什么你的子类 UINavigationController?如果您需要做的只是向它添加一个按钮,那么就不需要对它进行子类化。

设置一个顶部为 UINavigationController的层次结构,然后在根视图控制器的 viewDidLoad:方法中: 设置按钮并通过调用

[[self navigationItem] setRightBarButtonItem:myBarButtonItem];

尝试将按钮添加到视图控制器的 NavigationItem 中,该按钮将被推送到您创建的这个 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];

现在,以编程方式创建的 infoButton 将显示在导航栏中。其思想是导航控制器从即将显示的 UIViewController中获取显示信息(标题、按钮等)。您实际上不会直接向 UINavigationController添加按钮。

@ Artilheiro: 如果它是一个基于导航的项目,你可以创建 BaseViewController。所有其他视图将继承此 BaseView。在 BaseView 中,可以定义添加右按钮或更改左按钮文本的通用方法。

例如:

@ interface BaseController: UIViewController {

} - (void) setBackButtonCaption: (NSString *)标题;

(void) setRightButtonCaption: (NSString *)标题选择器: (SEL)选择器;

@ 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:@“ Forgot PIN”selectot: sel ] ;

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];

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

- (void)viewDidLoad {
[super viewDidLoad];


UIBarButtonItem *refreshButton = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:@selector(refreshClicked:)] autorelease];
self.navigationItem.rightBarButtonItem = refreshButton;


}


- (IBAction)refreshClicked:(id)sender {


}

你可以用这个:

目标 C

UIBarButtonItem *rightSideOptionButton = [[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(rightSideOptionButtonClicked:)];
self.navigationItem.rightBarButtonItem = rightSideOptionButton;

斯威夫特

let rightSideOptionButton = UIBarButtonItem()
rightSideOptionButton.title = "Right"
self.navigationItem.rightBarButtonItem = rightSideOptionButton
    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");
}
-(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];


}
    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];
}

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

以下是 Swift 中的解决方案(根据需要设置选项) :

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

试试这个,我喜欢。

导航栏 ,还在右键添加了背景图像。

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

似乎有些人(比如我)会来这里寻找如何在 Interface Builder 中添加导航栏按钮。下面的答案展示了如何做到这一点。

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

选择您的视图控制器,然后在 Xcode 菜单中选择 编辑器 > 嵌入 > 导航控制器

enter image description here

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

添加条形按钮项

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

enter image description here

它应该是这样的:

enter image description here

设置属性

您可以双击“ Item”将文本更改为类似于“ Refresh”的内容,但是您可以使用一个实际的 刷新图标。只需为 UIBarButtonItem选择属性检查器,为 系统项目选择 刷新

enter image description here

这将为您提供默认的刷新图标。

enter image description here

添加 IB 操作

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

class ViewController: UIViewController {


@IBAction func refreshBarButtonItemTap(sender: UIBarButtonItem) {
        

print("How refreshing!")
}
    

}

就是这样。

快速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

你可以试试

self.navigationBar.topItem.rightBarButtonItem = anotherButton;

只要复制并粘贴这个 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
}

迅捷4:

override func viewDidLoad() {
super.viewDidLoad()


navigationItem.leftBarButtonItem = UIBarButtonItem(title: "tap me", style: .plain, target: self, action: #selector(onButtonTap))
}


@objc func onButtonTap() {
print("you tapped me !?")
}

如果我们删除视图控制器或者尝试在 Interface Builder 中添加新的视图控制器(main.storboard) ,就会出现这个问题。要修复此问题,需要在新视图控制器中添加“导航项”。有时,我们创建新的视图控制器屏幕,它不会自动连接到“导航项”。

  1. 转到主线,故事板。
  2. 选择新的视图 Controller。
  3. 转到文档大纲。
  4. 检查查看 Controller 内容。
  5. 如果新的视图控制器没有导航项,则从以前的视图控制器中复制导航项,并将其粘贴到新的视图控制器中。
  6. 保存和清理项目。

您还可以使用 rightBarButtonItems添加多个按钮

-(void)viewDidLoad{


UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"button 1" style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD1:)];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"button 2" style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD2:)];


self.navigationItem.rightBarButtonItems = @[button1, button2];
}