如何在iPhone的导航栏中添加右键控制按钮

时间:2011-04-29 12:37:18

标签: ios objective-c iphone navigationbar

我想在导航栏中添加一个右侧栏按钮项,这样点击它就可以执行某项功能。

我创建了以下代码来添加正确的栏按钮项,但完成后,栏按钮项目不会显示在导航栏中:

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

9 个答案:

答案 0 :(得分: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)];

使用您自己的viewcontroller而不是navigationcontroller来引用navigationItem。

答案 1 :(得分:27)

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

尝试其他答案。我发布了这个答案,如果你的viewcontroller没有我认为是问题的导航控制器,它会起作用。

答案 2 :(得分:7)

在viewdidload

中添加此代码
UIBarButtonItem *chkmanuaaly = [[UIBarButtonItem alloc]initWithTitle:@"Calculate" style:UIBarButtonItemStylePlain target:self action:@selector(nextview)];
self.navigationItem.rightBarButtonItem=chkmanuaaly;
[chkmanuaaly release];

答案 3 :(得分:3)

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

//来自self(又称呼叫控制器):

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

现在,获得批准,这对János的答案来说似乎是多余的,但我需要更多的代表点来表明他的回应。 ; - )

答案 4 :(得分: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];

}

答案 5 :(得分:1)

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

答案 6 :(得分:1)

使用以下代码:

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

希望这可以帮助你。

享受!

答案 7 :(得分: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];
}

答案 8 :(得分:0)

夫特:

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

func settingsButtonTapped() {
    // Implement action 
}