0

Is it possible to create a UIControlEvents variable from a NSString or similar? So for example, I might have a NSString that is called UIControlEventTouchUpInside which is the same as one of the typedefs for the UIControlEvents variable.

Thanks for all help!

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
ManOx
  • 1,935
  • 5
  • 23
  • 37

3 Answers3

1

If I understand how this works correctly, UIControlEvents are enumerated as follows:

enum {
   UIControlEventTouchDown           = 1 <<  0,
   UIControlEventTouchDownRepeat     = 1 <<  1,
   UIControlEventTouchDragInside     = 1 <<  2,
   UIControlEventTouchDragOutside    = 1 <<  3,
   UIControlEventTouchDragEnter      = 1 <<  4,
   UIControlEventTouchDragExit       = 1 <<  5,
   UIControlEventTouchUpInside       = 1 <<  6,
   UIControlEventTouchUpOutside      = 1 <<  7,
   UIControlEventTouchCancel         = 1 <<  8,

   UIControlEventValueChanged        = 1 << 12,

   UIControlEventEditingDidBegin     = 1 << 16,
   UIControlEventEditingChanged      = 1 << 17,
   UIControlEventEditingDidEnd       = 1 << 18,
   UIControlEventEditingDidEndOnExit = 1 << 19,

   UIControlEventAllTouchEvents      = 0x00000FFF,
   UIControlEventAllEditingEvents    = 0x000F0000,
   UIControlEventApplicationReserved = 0x0F000000,
   UIControlEventSystemReserved      = 0xF0000000,
   UIControlEventAllEvents           = 0xFFFFFFFF
};

So once again, this is if I'm understanding this correctly so correct me if I'm wrong, but you should be able to make a variable integer and pass it to the control event like so:

    int myVariable = 64;
    [myButton addTarget:self action:@selector(mySelector) forControlEvents:myVariable];

See this link for explanation of Bitwise Shift Left

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Ok, so this works. However I don't believe that the event is correct. Because it seems to only work for UIControlEventTouchDragInside instead of UIControlEventTouchUpInside when I have 6 as the event. – ManOx Aug 16 '12 at 21:56
  • @ManOx Sorry, for this example touchUpInside would be 64. Additionally, see my edit for a link to a site describing the proper usage of the bitwise shift left operator. – Mick MacCallum Aug 16 '12 at 22:59
  • Perfect. I actually ended up just doing a bit wise shift like so: UIControlEvents eventForSelector = 1 << 6; Also, I'm wondering about how to do these type of events: 0xFFFFFFFF? – ManOx Aug 17 '12 at 00:14
  • and you can pass those directly to the button as well, e.g: `forControlEvents:0xFFFFFFFF` – Mick MacCallum Aug 17 '12 at 00:15
  • @NSPostWhenldle thanks, but what type of variable is that? It appears to be hex, but how can I declare a hex value in Objective-C? – ManOx Aug 17 '12 at 01:43
  • @ManOx I'm sorry to say, I don't have the answer to that question. – Mick MacCallum Aug 17 '12 at 01:44
  • `0xFFFFFFFF` is a literal 64-bit integer, just like `4294967295`. They have the same value, but one is written in hexadecimal and one is written in decimal. You can initialize or assign either representation to a regular `NSUInteger`. @ManOx – jscs Aug 17 '12 at 06:28
1

You'd have to create your own dictionary to map from one to the other:

NSDictionary * const controlEventTypesFromStrings = @{
     @"UIControlEventTouchDown" : @(UIControlEventTouchDown),
     @"UIControlEventTouchDownRepeat" : @(UIControlEventTouchDownRepeat),

     //etc.

This is the still-newish Clang literal syntax; you can of course make the dictionary using the old/standard [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInteger:UIControlEventTouchDown], @"UIControlEventTouchDown", ...

Then with your string, you'll get an NSNumber from the dictionary:

NSNumber * eventTypeNum = controlEventTypesFromStrings[stringDescribingControlEvent];
UIControlEvents eventType = [eventTypeNum unsignedIntegerValue];

Retrieving the NSNumber would be done via objectForKey: if you're not using the new subscripting syntax.

jscs
  • 63,694
  • 13
  • 151
  • 195
0

Here's some quick code that describes the bits in a UIControlEvents bitmask:

- (NSString *)describeEvents:(UIControlEvents)event {
    NSDictionary *events = @{@0: @"UIControlEventTouchDown",
                             @1: @"UIControlEventTouchDownRepeat",
                             @2: @"UIControlEventTouchDragInside",
                             @3: @"UIControlEventTouchDragOutside",
                             @4: @"UIControlEventTouchDragEnter",
                             @5: @"UIControlEventTouchDragExit",
                             @6: @"UIControlEventTouchUpInside",
                             @7: @"UIControlEventTouchUpOutside",
                             @8: @"UIControlEventTouchCancel",

                             @12: @"UIControlEventValueChanged",
                             @13: @"UIControlEventPrimaryActionTriggered",

                             @16: @"UIControlEventEditingDidBegin",
                             @17: @"UIControlEventEditingChanged",
                             @18: @"UIControlEventEditingDidEnd",
                             @19: @"UIControlEventEditingDidEndOnExit"};

    NSMutableString *result = [[NSMutableString alloc] init];
    for(int i = 0; i<sizeof(UIControlEvents) * CHAR_BIT; i++) {
        if((event & ((NSUInteger)1 << i)) != 0) {
            NSString *eventName = [events objectForKey:[NSNumber numberWithInt:i]];
            if(!eventName)
                eventName = [NSString stringWithFormat:@"%i", i];

            if(result.length > 0)
                [result appendString:@", "];
            [result appendString:eventName];
        }
    }

    return result;
}

Example output:

[self describeEvents:UIControlEventTouchCancel] // UIControlEventTouchCancel

[self describeEvents:UIControlEventAllTouchEvents] // UIControlEventTouchDown, UIControlEventTouchDownRepeat, UIControlEventTouchDragInside, UIControlEventTouchDragOutside, UIControlEventTouchDragEnter, UIControlEventTouchDragExit, UIControlEventTouchUpInside, UIControlEventTouchUpOutside, UIControlEventTouchCancel, 9, 10, 11

[self describeEvents:UIControlEventAllEvents] // UIControlEventTouchDown, UIControlEventTouchDownRepeat, UIControlEventTouchDragInside, UIControlEventTouchDragOutside, UIControlEventTouchDragEnter, UIControlEventTouchDragExit, UIControlEventTouchUpInside, UIControlEventTouchUpOutside, UIControlEventTouchCancel, 9, 10, 11, UIControlEventValueChanged, UIControlEventPrimaryActionTriggered, 14, 15, UIControlEventEditingDidBegin, UIControlEventEditingChanged, UIControlEventEditingDidEnd, UIControlEventEditingDidEndOnExit, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
KyotoFox
  • 40
  • 5