10

I want to pass BOOL to [NSArray makeObjectsPerformSelector:withObject:] as a parameter. E.g.

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

The above code won't work because withObject only accepts id.

What's the right way to do it?

I seen some code with this:

[buttons makeObjectsPerformSelector:@selector(setEnabled:) withObject: (id)kCFBooleanTrue];
[buttons makeObjectsPerformSelector:@selector(setEnabled:) withObject: (id)kCFBooleanFalse];

This works fine on 4.2 simulator but fails on 4.2 iphone.

Jonathan.
  • 53,471
  • 47
  • 180
  • 281
PokerIncome.com
  • 1,700
  • 2
  • 19
  • 29

4 Answers4

10

You could write a UIButton (or even UIView) category that allows you to use setEnabled: with an object.

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

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

and then you could use

[buttons makeObjectsPerformSelector:@selector(setEnabledWithNSNumber:) withObject:[NSNumber numberWithBool:NO]];
[buttons makeObjectsPerformSelector:@selector(setEnabledWithNSNumber:) withObject:[NSNumber numberWithBool:YES]];
Matthias Bauch
  • 88,907
  • 20
  • 222
  • 246
6

I recall one had to do something else than just withObject:@YES but since I can't find it anymore I figured out it works as well with

[buttons enumerateObjectsUsingBlock:^(NSButton *item, NSUInteger idx, BOOL *stop) 
    {[item setEnabled:YES];}];

Or the faster/older/readabler :) way:

for (NSButton *item in buttons) {[item setEnabled:YES];};

One should know that enumerateObjectsUsingBlock isn't particularily fast, but it shouldn't be a huge killer here anyways :) If you want fast you can also do that with a for (;;) block, sure :)

StuFF mc
  • 4,017
  • 2
  • 30
  • 31
3

Please take a look at the following duplicate questions:

How to use performSelector:withObject:afterDelay: with primitive types in Cocoa?.

Using performSelector:withObject:afterDelay: with non-object parameters

SEL performSelector and arguments

Hope it helps.

Community
  • 1
  • 1
visakh7
  • 26,340
  • 8
  • 54
  • 69
0

If you are Passing the BOOL parameters in static then my answer in link will be helpful..

Community
  • 1
  • 1
Ashok
  • 5,325
  • 5
  • 47
  • 78