代码之家  ›  专栏  ›  技术社区  ›  frankodwyer

如何以编程方式替换内置ib的uitoolbar项

  •  12
  • frankodwyer  · 技术社区  · 15 年前

    我有一个工具栏,上面有各种各样的图像按钮,都是在Interface Builder中创建的。

    我希望能够以编程方式在按下时用活动指示器替换其中一个按钮,然后放回原来的按钮,但在活动完成时将其颜色从白色更改为黄色。

    使用ib构建的工具栏是否可能实现这一点,或者我必须考虑以编程方式和自定义视图构建整个工具栏吗?

    8 回复  |  直到 8 年前
        1
  •  26
  •   Aaron    15 年前

    下面是我在类似情况下所做的一个例子。我想使用InterfaceBuilder构建工具栏,但是根据它是否被“选中”来切换其中一个barbuttonities。在这个例子中,有一些关键的事情需要注意。为头文件中的类定义了以下2个成员变量:

    NSMutableArray *toolbarItems;
    IBOutlet UIToolbar *toolbar;
    NSUInteger checkUncheckIndex;
    

    当我想更新检查状态时,我调用这个函数…请注意,在单击uitoolbar中的特定按钮时,会调用一个名为checkuncheckclicked的选择器。而uitoolbar被设置为iboutlet to工具栏。或者,您可以将uibarbuttonitem连接为一个出口本身,并使用它作为逻辑来标识按钮的索引,或者就此而言,如果事情不会随着时间而改变,您可以硬编码按钮的索引。最后,有一个checked.png和unchecked.png可以在项目中包含的选项之间进行替换。

    - (void)updateBarButtonItemChecked:(BOOL)checked {
        if (toolbarItems == nil) {
            toolbarItems = [[NSMutableArray arrayWithArray:toolbar.items] retain];
            checkUncheckIndex = -1;
    
            for (NSUInteger i = 0; i < [toolbarItems count]; i++) {
                UIBarButtonItem *barButtonItem = [toolbarItems objectAtIndex:i];
                if (barButtonItem.action == @selector(checkUncheckClicked)) {
                    favoriteIndex = i;
                    break;
                }
            }
        }
    
        if (checkUncheckIndex != -1) {
            UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:checked ? @"checked.png" : @"unchecked.png"] 
                                                                               style:UIBarButtonItemStylePlain target:self action:@selector(checkUncheckClicked)] autorelease];
            [toolbarItems replaceObjectAtIndex:checkUncheckIndex withObject:barButtonItem];
    
            toolbar.items = toolbarItems;
        }
    }
    

    当然,toolbaritems和toolbar应该在类的dealoc中释放。

    希望这有帮助!

        2
  •  2
  •   wagashi    13 年前

    这是我使用的方法 完全通过编程来管理工具栏似乎要简单得多,所以……

    在视图控制器中,声明一组或多组uibarButtonItem项作为属性项,还声明工具栏并将其作为uitoolbar属性挂接。同时声明一个或多个数组来保存这些项。

    在实施中 例如,在视图中加载alloc并设置uibarbuttonities

           playButton = [[UIBarButtonItem alloc]
             initWithBarButtonSystemItem:UIBarButtonSystemItemPlay 
        target:self 
    action:@selector(handlePlayClick)];
    

    柔性按钮(用于对齐等)声明如下

       flexButton1 =[[UIBarButtonItem alloc]
     initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
    target:nil action:nil];
    

    有几种initmethods来处理工具栏支持的不同类型的按钮。所有这些都遵循与上面类似的语法。值得注意的是目标和动作设置。目标:通常是自己,操作是按钮应该触发的函数的名称。

    分配完uibarButton后,使用initWithObjects将它们添加到数组中。

    然后将按钮分配到您要调用的工具栏

    [toolbar setItems:<array name>];
    

    别忘了在代码末尾释放uibarbuttons和数组。

    希望这有帮助。如果你需要更多的代码,请告诉我。

    富D

        3
  •  2
  •   SwiftArchitect    9 年前

    迅捷

    自从这些答案公布以来,情况发生了很大变化。这里有一个Swift 2.0解决方案。

    不管原版是什么 UIBarButtonItem 您正在替换的已以编程方式创建。你只需要 UIToolbar 出口。要替换左侧的第2项,请执行以下操作:

    var toolbarItems = self.toolbar.items
    let newItem = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "play")
    toolbarItems![1] = newItem
    self.toolbar.setItems(toolbarItems, animated: true)
    
        4
  •  2
  •   Mark Bourke    8 年前

    斯威夫特有一个更简单的方法。在我的例子中,每次触摸都会切换播放按钮和暂停按钮。

    @IBAction func playandPause(sender: UIBarButtonItem) { // Here we are creating an outlet. Hook this up to the bar button item.
        for (index, button) in toolbarItems!.enumerate() { // Enumerating through all the buttons
            if button === sender { // === operator is used to check if the two objects are the exact same instance in memory.
                var barButtonItem: UIBarButtonItem!
                if mediaplayer.playing {
                    mediaplayer.pause()
                    barButtonItem = UIBarButtonItem.init(barButtonSystemItem: .Play, target: self, action: #selector(playandPause(_:)))
                }else {
                    mediaplayer.play()
                    barButtonItem = UIBarButtonItem.init(barButtonSystemItem: .Pause, target: self, action: #selector(playandPause(_:)))
                }
                toolbarItems![index] = barButtonItem // Replace the old item with the new item.
                break // Break once we have found the button as it is unnecessary to complete the rest of the loop
            }
        }
    }
    
        5
  •  1
  •   drvdijk bobDevil    15 年前

    我认为这是可能的。您可以尝试在ViewController类中为该特定按钮创建一个ib outlet,并将该按钮从ib连接到该插座,或者可以使用该uitoolbar实例的items属性(您确实引用了它,不是吗?)然后在其中找到适当的项,用修改过的项创建一个新的nsarray,并将其设置回toolbar.items属性。

    高温高压

        6
  •  0
  •   theLastNightTrain    15 年前

    上面有一个提示-工具栏会去掉图标中的任何颜色,所以你仍然无法使其变为黄色。您需要更改图像形状以指示“开”。

    或者,您需要使用uibuttons(使用initWithCustomView)加载barbuttons,并适当地设置按钮的图像。

    高温高压

        7
  •  0
  •   Wanderer    12 年前

    尝试:

    - (void)setTitle:(NSString *)title forItem:(int)item ofToolbar:(UIToolbar *)tb {
        NSMutableArray *newItems = [tb.items mutableCopy];
        UIBarButtonItem *old = [newItems objectAtIndex:1],
        *titled = [[UIBarButtonItem alloc] initWithTitle:title style:old.style target:old.target action:old.action];
    
        [newItems  replaceObjectAtIndex:1 withObject:titled];
        tb.items = newItems;
    
        [titled release];
    }
    
        8
  •  0
  •   Phiter    8 年前

    对于在ib中创建的整个工具栏(也就是说,不是单个工具栏项),创建一个iboutlet:

    @IBOutlet weak var toolbarThatYouMade: UIToolbar!
    

    这是单个工具栏项的数组,因此您可以使用 [0] 索引:

    toolbarThatYouMade.items![0].image = UIImage(named: "New Image")
    

    此代码假定您的资产中有一个名为“新映像”的映像。

    然后,您可以触发工具栏项的图像更改,而无需访问该特定项本身;例如,如果您将此项用于媒体播放器之类的设备,则可以从以下位置切换暂停/播放图像:
    a)暂停/播放按钮本身,
    b)停止按钮,
    c)当玩家状态发生变化时,

    d) 完全是别的东西 . (勇敢点!大胆点!是以“b”开头的另一个词!!)