【问题标题】:How to add UIBarButtonItem in UIToolBar in code如何在代码中的 UIToolBar 中添加 UIBarButtonItem
【发布时间】:2011-03-21 21:23:37
【问题描述】:

我有标准 UIBarButtonItem

UIBarButtonItem *share = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(share:)];

如何将她添加到 UIToolBar?我试过了

self.toolbarItems = [NSArray arrayWithObject:share];

但它不起作用。需要你的帮助。

【问题讨论】:

    标签: objective-c xcode ios uibarbuttonitem


    【解决方案1】:

    你能比“它不起作用”更具体吗?

    如果您尝试将项目添加到已有项目的工具栏,则需要修改项目数组:

    NSMutableArray *newItems = [self.toolbarItems mutableCopy];
    [newItems addObject:share];
    self.toolbarItems = newItems;
    

    【讨论】:

    • 对不起。这意味着工具栏出现时没有任何按钮。现在试着走自己的路
    • 寻找解决方案 [self setToolbarItems:[NSArray arrayWithObject:share]];
    【解决方案2】:

    确保您已将工具栏设为 IBOutlet 或以编程方式添加工具栏

    IBOutlet UIToolbar *toolBar;
    
    UIBarButtonItem *infoButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"info" style:UIBarButtonItemStyleBordered  target:self action:@selector(infoButtonClicked)];
    
    toolBar.items = [NSArray arrayWithObjects:infoButtonItem, nil];
    

    【讨论】:

      【解决方案3】:

      确保工具栏没有隐藏;您可以尝试将以下内容添加到您的视图控制器的 viewWillAppear:animated: 方法中:

      [self.navigationController setToolbarHidden:NO animated:YES];
      

      【讨论】:

        【解决方案4】:

        [toolbar setItems:[NSArray arrayWithObject:share] animated:YES];

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-01-04
          • 2010-11-07
          • 2011-12-01
          • 1970-01-01
          • 2011-01-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多