objective-c - 如何使用标签为动态创建的按钮动态设置背景图像?

标签 objective-c ios xcode4

我在 View 中动态创建了一行数字按钮。单击任何数字时,按钮都会突出显示。如果我单击该行中的多个按钮,则所有单击的按钮都会突出显示。如何避免多个按钮高度化?

我使用的代码如下

-(void)pressed:(id)sender{
    UIButton *button = (UIButton *)sender;
    if(!button.selected){

        [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(highlightButton:) userInfo:button repeats:NO];        

    } else {
        [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(unhighlightButton:) userInfo:button repeats:NO];
    }
-(void)highlightButton:(id)sender{
    UIButton *button = (UIButton *)[sender userInfo];
    button.highlighted = YES;
    button.selected = YES;
}
-(void)unhighlightButton:(id)sender{
    UIButton *button = (UIButton *)[sender userInfo];
    button.highlighted = NO;
    button.selected = NO;
}

最佳答案

我假设您的意思是您点击的每个按钮都会突出显示,而不会删除之前的突出显示。

一次仅突出显示一个按钮。跟踪突出显示的按钮,并在点击另一个按钮时删除其突出显示。

- (void)buttonTapped:(UIButton *)button {
    if (button != [self lastSelectedButton]) { // don't re-highlight the same button
        // remove the highlight of "lastSelectedButton"

        [self setLastSelectedButton:button];
        // add the highlight to "lastSelectedButton" (not updated to the new button)
    }

    // Do the rest of you button logic here ...
}

关于objective-c - 如何使用标签为动态创建的按钮动态设置背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10310913/

相关文章:

objective-c - Objective-C 中的子类化是一种不好的做法吗?

objective-c - 如何在 Cocoa 应用程序中将 NSView 添加到 NSWindow?

ios - GPU Image2 音频问题

ios - 如何在 iBook 中像打开书一样制作动画?

objective-c - Objective-C : Getting the height and width of a UIView accurately

ios - 为什么我不能将我的 NSDictionary 数据放入 UITableView

iphone - 收到内存警告和应用程序崩溃

iOS5 -webkit-overflow-scrolling 导致触摸事件停止工作

c++ - 无法针对 OS X 上的 Boost.log 进行编译

ios - 如何使用设备而不是模拟器