如何向UINavigationController添加右键?

Art*_*iro 190 iphone uinavigationbar uinavigationcontroller ios

我试图在导航控制器的顶部栏添加刷新按钮但没有成功.

这是标题:

@interface PropertyViewController : UINavigationController {

}
Run Code Online (Sandbox Code Playgroud)

以下是我尝试添加它的方法:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

Lou*_*arg 362

尝试在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];
}
Run Code Online (Sandbox Code Playgroud)

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

  • 我在UINavigationController子类之外执行此操作,您可以像这样引用它:someNavController.topLevelController.navigationItem.rightBarButtonItem = anotherButton (45认同)
  • 即使在UINavigationContoller的viewDidLoad方法内部,我也必须将其称为`self.topViewController.navigationItem.leftBarButtonItem = nextButton;` (10认同)
  • 我正在实现建议的解决方案,我的应用程序一直崩溃,直到我意识到@selector方法名称必须以冒号(:)结束. (2认同)

小智 38

尝试将按钮添加到视图控制器的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];
Run Code Online (Sandbox Code Playgroud)

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

  • 为__"+1"尝试将按钮添加到将要推送的视图控制器的navigationItem"__! (4认同)
  • 直接在UINavigationController上设置rightBarButtonItem对我不起作用(如上面的Louis Gerbarg所提出的).而是直接在UIViewController上设置项目,就像Adem建议的那样有效! (2认同)

Sur*_*gch 25

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

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

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

在此输入图像描述

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

添加条形按钮项

将a UIBarButtonItem从对象库拖到顶部导航栏.

在此输入图像描述

它应该如下所示:

在此输入图像描述

设置属性

你可以双击"项目"来改变文字的东西,如"刷新",但对于实际的图标刷新,您可以使用.只需UIBarButtonItem系统项选择属性检查器,然后选择" 刷新".

在此输入图像描述

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

在此输入图像描述

添加IB动作

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

class ViewController: UIViewController {

    @IBAction func refreshBarButtonItemTap(sender: UIBarButtonItem) {

        print("How refreshing!")
    }

}
Run Code Online (Sandbox Code Playgroud)

而已.


Man*_*nni 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 {

}
Run Code Online (Sandbox Code Playgroud)


Van*_*hta 11

你可以用这个:

Objective-C的

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

迅速

let rightSideOptionButton = UIBarButtonItem()
rightSideOptionButton.title = "Right"
self.navigationItem.rightBarButtonItem = rightSideOptionButton
Run Code Online (Sandbox Code Playgroud)


Gau*_*ani 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];

}
Run Code Online (Sandbox Code Playgroud)


fan*_*dro 6

对于swift 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
Run Code Online (Sandbox Code Playgroud)


小智 6

你可以试试

self.navigationBar.topItem.rightBarButtonItem = anotherButton;
Run Code Online (Sandbox Code Playgroud)


小智 5

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

var optionButton = UIBarButtonItem()
optionButton.title = "Settings"
//optionButton.action = something (put your action here)
self.navigationItem.rightBarButtonItem = optionButton
Run Code Online (Sandbox Code Playgroud)