按钮离开突出显示状态时的调用方法

时间:2012-04-01 21:13:34

标签: iphone ios uibutton

我想在突出显示按钮时执行操作,并在离开突出显示状态时执行其他操作。有什么建议?

2 个答案:

答案 0 :(得分:13)

你可以使用KVO

[button addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];

然后

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([@"highlighted" isEqualToString:keyPath]) {

        NSNumber *new = [change objectForKey:NSKeyValueChangeNewKey];
        NSNumber *old = [change objectForKey:NSKeyValueChangeOldKey];

        if (old && [new isEqualToNumber:old]) {
            NSLog(@"Highlight state has not changed");
        } else {
            NSLog(@"Highlight state has changed to %d", [object isHighlighted]);
        }
    }
}

您只关心这些更改,每次状态更改时都会调用此更改,例如如果你移动选择然后用手指按住按钮

答案 1 :(得分:1)

喜欢这个吗?

self.testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.testButton addTarget:self action:@selector(methodOne) forControlEvents:UIControlEventTouchDown];
[self.testButton addTarget:self action:@selector(methodTwo) forControlEvents:UIControlEventTouchUpInside];
相关问题