【问题标题】:How to add a right bar button to a navigation bar in iphone如何在iphone的导航栏中添加右栏按钮
【发布时间】:2011-08-15 11:35:08
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios objective-c iphone navigationbar


    【解决方案1】:

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

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

    【讨论】:

    • 是的,这个改变正是我所需要的。
    【解决方案2】:
    -(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];
    }
    

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

    【讨论】:

    • UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Add:)]; app.navigationController.navigationItem.rightBarButtonItem=button; [button release]; 试试这个
    • 如果您通过导航控制器以编程方式调用视图控制器,则 [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBtnPressed)]];
    • 糟糕。我应该将我的视图控制器映射到 Storyboard 中的导航视图控制器吗?我正在使用 Xcode 8.2,iOs 10
    【解决方案3】:

    在 viewdidload 中添加这段代码

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

    【讨论】:

    • 我试过这个代码,但我的右键项目在导航栏中不可见
    • 我认为导航栏不是导航控制器的一部分。您是否将导航栏放在视图控制器的 xib 文件中?如果然后在 .xib 文件中添加按钮。希望对你有帮助
    【解决方案4】:

    这里的大多数答案都是在显示后从新 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];
    

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

    【讨论】:

      【解决方案5】:
      UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser)];
          self.navigationItem.rightBarButtonItem=add;
          [add release];
      

      【讨论】:

      • 我试过这个代码,但我的右键项目在导航栏中不可见
      【解决方案6】:

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

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

      【讨论】:

        【解决方案7】:

        使用以下代码:

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

        希望对你有所帮助。

        享受吧!

        【讨论】:

        • 我已添加此代码,但我的右栏按钮项在导航栏中不可见
        【解决方案8】:

        斯威夫特:

        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 
        }
        

        【讨论】:

          【解决方案9】:
          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];
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-08-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多