如何以编程方式替换IB中内置的UIToolBar项目 [英] How to programmatically replace UIToolBar items built in IB

查看:84
本文介绍了如何以编程方式替换IB中内置的UIToolBar项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有各种图像按钮的工具栏,在Interface Builder中创建。

I have a toolbar with various image buttons, created in Interface Builder.

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

I'd like to be able to programmatically replace one of the buttons with an activity indicator when pressed, and then put back the original button but change its color from white to yellow when the activity completes.

这是否可以使用IB构建的工具栏,或者我必须看看以编程方式构建整个工具栏和自定义视图?

Is this possible with an IB built toolbar or must I look at building the whole toolbar programmatically and custom views?

推荐答案

以下是我在类似情况下所做的一个示例。我想使用Interface Builder构建工具栏,但根据是否检查来切换其中一个BarButtonItem。在这个例子中,有几个关键的事情需要注意。为头文件中的类定义了以下2个成员变量:

Here is an example of what I did in a similar situation. I wanted to build the toolbar using Interface Builder but toggle one of the BarButtonItems based on whether or not it was "checked". In this example, there are a few key things to note. The following 2 member variables are defined for the class in the header file:

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

当我想更新已检查状态时,我调用此函数...请注意,有一个名为checkUncheckClicked的选择器,在单击UIToolbar中的特定按钮时调用。并且UIToolbar被设置为工具栏的IBOutlet。或者,您可以将UIBarButtonItem连接为插件本身,并将其用作逻辑来标识按钮的索引,或者就此而言,如果事情不会随时间发生变化,您可以对按钮的索引进行硬编码。最后,还有一个checked.png和unchecked.png在项目中包含的替换。

When I want to update the checked status, I call this function... Please note that there is a selector defined called checkUncheckClicked that is called when the particular button in the UIToolbar is clicked. And the UIToolbar is set up as an IBOutlet to toolbar. Alternately, you could hook up the UIBarButtonItem as an outlet itself and use that as your logic to identify the index of the button, or for that matter, you could hard-code the index of the button if things won't change over time. Finally, there is a checked.png and unchecked.png to alternate between that is included in the project.

- (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和工具栏应该在你的dealloc为班级。

And, of course toolbarItems and toolbar should be released in your dealloc for the class.

希望这会有所帮助!

这篇关于如何以编程方式替换IB中内置的UIToolBar项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆