我想将BOOL作为参数传递给[NSArray makeObjectsPerformSelector:withObject:]。例如。

[buttons makeObjectsPerformSelector:@selector(setEnabled:) withObject: NO];

上面的代码不起作用,因为withObject仅接受id。

什么是正确的方法?

我看到了一些与此相关的代码:
[buttons makeObjectsPerformSelector:@selector(setEnabled:) withObject: (id)kCFBooleanTrue];
[buttons makeObjectsPerformSelector:@selector(setEnabled:) withObject: (id)kCFBooleanFalse];

这在4.2模拟器上工作正常,但在4.2 iphone上失败。

最佳答案

您可以编写一个UIButton(甚至UIView)类别,该类别允许您将setEnabled:与一个对象一起使用。

@interface UIButton(setEnabledWithObject)
- (void)setEnabledWithNSNumber:(NSNumber *)bNum;
@end

@implementation UIButton(setEnabledWithObject)
- (void)setEnabledWithNSNumber:(NSNumber *)bNum {
    [self setEnabled:[bNum boolValue]];
}
@end

然后你可以使用
[buttons makeObjectsPerformSelector:@selector(setEnabledWithNSNumber:) withObject:[NSNumber numberWithBool:NO]];
[buttons makeObjectsPerformSelector:@selector(setEnabledWithNSNumber:) withObject:[NSNumber numberWithBool:YES]];

关于iphone - 将BOOL传递给makeObjectsPerformSelector :withObject:,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5374819/

10-10 13:30