192

我正在尝试将刷新按钮添加到导航控制器的顶部栏,但没有成功。

这是标题:

@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;
}
4

21 回答 21

363

尝试在 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 和视图加载之间发生了很多事情,你可能正在做一些导致 navigationItem 重置的事情之间。

于 2009-08-02T20:43:56.920 回答
38

尝试将按钮添加到将被推送到PropertyViewController您创建的此类的视图控制器的 navigationItem。

那是:

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.

于 2009-12-11T22:11:19.517 回答
25

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

将导航控制器添加到情节提要

选择您的 View Controller,然后在 Xcode 菜单中选择Editor > Embed In > Navigation Controller

在此处输入图像描述

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

添加条形按钮项

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

在此处输入图像描述

它应该如下所示:

在此处输入图像描述

设置属性

您可以双击“项目”将文本更改为“刷新”之类的内容,但是您可以使用一个实际的刷新图标。只需选择 Attributes InspectorUIBarButtonItem并为System Item选择Refresh

在此处输入图像描述

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

在此处输入图像描述

添加 IB 操作

控制从 拖到UIBarButtonItemView Controller 以添加@IBAction.

class ViewController: UIViewController {

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

就是这样。

于 2015-11-12T11:19:00.147 回答
14

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

- (void)viewDidLoad {
    [super viewDidLoad];

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

}

- (IBAction)refreshClicked:(id)sender {

}
于 2012-09-27T14:31:32.383 回答
11

你可以使用这个:

Objective-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
于 2013-07-24T13:01:41.790 回答
6
-(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];

}
于 2014-05-19T07:12:30.720 回答
6

对于快速 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
于 2016-05-03T07:42:33.447 回答
6

你可以试试

self.navigationBar.topItem.rightBarButtonItem = anotherButton;
于 2016-12-10T07:28:25.467 回答
5

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

var optionButton = UIBarButtonItem()
optionButton.title = "Settings"
//optionButton.action = something (put your action here)
self.navigationItem.rightBarButtonItem = optionButton
于 2015-06-20T19:41:54.147 回答
4

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

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

[[self navigationItem] setRightBarButtonItem:myBarButtonItem];
于 2009-08-02T23:38:50.773 回答
3

斯威夫特 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 !?")
}
于 2018-01-11T11:53:00.300 回答
2
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];
于 2012-09-12T07:04:53.970 回答
2

试试这个。它对我有用。

导航栏和右键添加背景图片。

 UIBarButtonItem *Savebtn=[[UIBarButtonItem alloc]initWithImage:[[UIImage    
 imageNamed:@"bt_save.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] 
 style:UIBarButtonItemStylePlain target:self action:@selector(SaveButtonClicked)];
 self.navigationItem.rightBarButtonItem=Savebtn;
于 2015-09-25T07:36:58.130 回答
0
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
于 2013-09-03T11:13:44.847 回答
0
- (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");  
}
于 2013-10-25T05:11:02.163 回答
0
    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];
}
于 2014-12-15T06:57:57.007 回答
0

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

于 2015-02-20T09:41:19.453 回答
0

只需复制并粘贴此 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
}
于 2017-05-29T09:43:11.093 回答
0

如果我们删除视图控制器或尝试在界面构建器(main.storyboard)中添加新的视图控制器,则可能会出现此问题。要解决此问题,需要在新视图控制器中添加“导航项”。有时会发生我们创建新的视图控制器屏幕并且它不会自动连接到“导航项”。

  1. 转到 main.storyboard。
  2. 选择那个新的视图控制器。
  3. 转到文档大纲。
  4. 检查视图控制器内容。
  5. 如果新的视图控制器没有导航项,则从以前的视图控制器复制导航项并将其粘贴到新的视图控制器中。
  6. 保存并清理项目。
于 2018-04-12T01:05:41.523 回答
0

您还可以使用添加多个按钮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];
}
于 2018-10-10T11:11:00.900 回答
-1

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

前任:

@interface BaseController : UIViewController {

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

(void) setRightButtonCaption:(NSString *)caption selectot:(SEL)selector;

@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 登录视图 : BaseController {

在某些方法中调用基本方法为:

SEL sel= @selector(switchToForgotPIN);

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

于 2010-05-13T05:18:22.477 回答