26

我想在导航栏中添加一个右栏按钮项,以便在单击时执行某些功能。

我创建了以下代码来添加右侧栏按钮项,但完成后,栏按钮项未显示在导航栏中:

-(void)viewDidload{
    self.navigationItem.rightBarButtonItem = 
    [[[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemAdd                                                                                                     
                                target:self
                                action:@selector(Add:)] autorelease];    
}

-(IBAction)Add:(id)sender
{
    TAddNewJourney *j=[[TAddNewJourney alloc]init];
    [app.navigationController pushViewController:j animated:YES];
    [j release];
}
4

9 回答 9

39

假设,你有一个 UIViewController,像这样:

UIViewController *vc = [UIViewController new];

您将其添加到导航控制器:

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];

这样 rightBarButtonItem 将不会显示

nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)];

但是这样它会出现

vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)];

使用您自己的视图控制器而不是导航控制器来引用导航项。

于 2012-11-10T11:23:21.450 回答
28
-(void)viewDidLoad
{ 
    [super viewDidLoad];
    app.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Add:)] autorelease];
}

-(IBAction)Add:(id)sender
{
    TAddNewJourney *j=[[TAddNewJourney alloc]init];
    [app.navigationController pushViewController:j animated:YES];
    [j release];
}

尝试其他答案。我发布了这个答案,以便如果您的视图控制器没有我认为是问题的导航控制器,它将起作用。

于 2011-04-29T12:43:07.140 回答
7

在 viewdidload 中添加此代码

UIBarButtonItem *chkmanuaaly = [[UIBarButtonItem alloc]initWithTitle:@"Calculate" style:UIBarButtonItemStylePlain target:self action:@selector(nextview:)];

self.navigationItem.rightBarButtonItem = chkmanuaaly;
于 2011-04-29T12:41:05.267 回答
3

此处的大多数答案都解决了在显示后从新 VC 中设置 UINavigationBar 的 rightBarButtonItem 的问题。我的需要,实际上是由 János 回答的,是从 CALLING UIViewController 设置 UINavigationItem 项。因此,下面的代码(谢谢,János)。

// 从自身内部(也就是调用控制器):

    UIViewController *c = [[[UIViewController alloc] init...] autorelease];
    c.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissModalViewControllerAnimated:)] autorelease];
    c.navigationItem.title = @"Title Goes Here";
    UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:c] autorelease];
    nav.navigationBarHidden = FALSE;
    [self presentModalViewController:nav animated:YES];

现在,当然,这对亚诺斯的回答来说似乎是多余的,但我需要更多的代表点来标记他的回答。;-)

于 2012-12-13T21:48:19.667 回答
2
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser)];
    self.navigationItem.rightBarButtonItem=add;
    [add release];
于 2011-04-29T12:39:51.460 回答
2

我想这将是更完整的响应,并且在任何情况下都应该有所帮助:

-(void)viewDidLoad{

    //Set Navigation Bar
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];

    //Set title if needed
    UINavigationItem * navTitle = [[UINavigationItem alloc] init];
    navTitle.title = @"Title";

    //Here you create info button and customize it
    UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeInfoLight];

    //Add selector to info button
    [tempButton addTarget:self action:@selector(infoButtonClicked) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];

    //In this case your button will be on the right side
    navTitle.rightBarButtonItem = infoButton;

    //Add NavigationBar on main view
    navBar.items = @[navTitle];
    [self.view addSubview:navBar];

}
于 2015-08-07T22:16:06.640 回答
1

使用以下代码:

    UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser)];
    self.navigationItem.rightBarButtonItem=add;
    [add release];

希望这可以帮助你。

享受!

于 2011-04-29T12:43:30.040 回答
1

迅速:

let rightButton = UIBarButtonItem(image: UIImage(named: "imageName"),
                                  style: .plain,
                                 target: self,
                                 action: #selector(buttonTapped))
navigationItem.setRightBarButton(rightButton, animated: false)

@objc func buttonTapped(sender: UIBarButtonItem!) {
    // Implement action 
}
于 2017-02-26T21:30:18.363 回答
0
UIButton *dButton=[UIButton buttonWithType:0];
dButton.frame=CGRectMake(50,50,50,50);

[dButton addTarget:self  action:@selector(clickdButton:)
  forControlEvents:UIControlEventTouchUpInside];
[dButton setImage:[UIImage imageNamed:@"iconnavbar.png"]
         forState:UIControlStateNormal];    
dButton.adjustsImageWhenHighlighted=NO;
dButton.adjustsImageWhenDisabled=NO;
dButton.tag=0;
dButton.backgroundColor=[UIColor clearColor];

UIBarButtonItem *RightButton=[[[UIBarButtonItem alloc] initWithCustomView:dButton]autorelease];
self.navigationItem.rightBarButtonItem=RightButton;

进而:

-(IBAction)clickdButton:(id)sender{
    classexample *tempController=[[classexample alloc] init];
    [self.navigationController pushViewController:tempController animated:YES];
    [tempController autorelease];
}
于 2011-05-05T14:52:14.637 回答