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

发布于 2024-11-03 18:33:52 字数 658 浏览 4 评论 0原文

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

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

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

I want to add a right bar button item to the navigation bar, so that on click, it performs certain a function.

I have created the following code to add the right bar button item, but after it is done, the bar button item is not getting displayed in navigation bar:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

潦草背影 2024-11-10 18:33:52

假设您有一个 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)];

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

Let assume, you have a UIViewController, like this:

UIViewController *vc = [UIViewController new];

And you added it to navigation controller:

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

In this way rightBarButtonItem will NOT getting displayed:

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

BUT this way IT WILL appear:

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

Use your own viewcontroller instead of the navigationcontroller to refer navigationItem.

微暖i 2024-11-10 18:33:52
-(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];
}

尝试其他答案。我发布了这个答案,这样如果您的视图控制器没有导航控制器(我认为这是问题所在),它就可以工作。

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

Try the other answers. I posted this answer so that it will work if your viewcontroller does not have a navigation controller which i think is the problem.

寄与心 2024-11-10 18:33:52

在viewdidload中添加这段代码

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

self.navigationItem.rightBarButtonItem = chkmanuaaly;

Add this code in viewdidload

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

self.navigationItem.rightBarButtonItem = chkmanuaaly;
心作怪 2024-11-10 18:33:52

这里的大多数答案都解决了在显示 UINavigationBar 的 rightBarButtonItem 后在新 VC 中设置它的问题。我的需求(实际上由 János 回答)是从调用 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];

现在,理所当然,这对于 János 的回答来说似乎是多余的,但我需要更多的代表点才能标记他的回答。 ;-)

Most of the answers here address setting the UINavigationBar's rightBarButtonItem from within the new VC after it is shown. My need, which was actually answered by János, is to set the UINavigationItem items from the CALLING UIViewController. Hence, the following code (Thanks, János).

// from within self (aka the calling controller):

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

Now, granted, this may seem redundant to János' answer, but I need more rep points in order o mark his response up. ;-)

尐偏执 2024-11-10 18:33:52
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser)];
    self.navigationItem.rightBarButtonItem=add;
    [add release];
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser)];
    self.navigationItem.rightBarButtonItem=add;
    [add release];
山有枢 2024-11-10 18:33:52

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

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

}

I guess it will be more complete response and it should help in any situation:

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

}
染墨丶若流云 2024-11-10 18:33:52

使用以下代码:

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

希望这可以帮助您。

享受!

Use the following code:

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

Hope this helps you out.

Enjoy!

滥情哥ㄟ 2024-11-10 18:33:52

迅速:

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 
}

Swift:

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 
}
好倦 2024-11-10 18:33:52
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];
}
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;

and then:

-(IBAction)clickdButton:(id)sender{
    classexample *tempController=[[classexample alloc] init];
    [self.navigationController pushViewController:tempController animated:YES];
    [tempController autorelease];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文