来自字符串的UIControlEvents变量

时间:2012-08-16 18:44:54

标签: objective-c ios xcode nsstring uicontrolevents

是否可以从UIControlEvents或类似内容创建NSString变量?例如,我可能有一个名为NSString的{​​{1}},它与UIControlEventTouchUpInside变量的typedef之一相同。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

如果我理解这是如何正常工作的,那么UIControlEvents的枚举如下:

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
};

所以再一次,如果我正确理解这一点,那么如果我错了就纠正我,但你应该能够创建一个变量整数并将其传递给控件事件,如下所示:

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

有关Bitwise Shift Left

的说明,请参阅此链接

答案 1 :(得分:1)

您必须创建自己的字典才能从一个字体映射到另一个:

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

     //etc.

这是仍然新鲜的Clang literal syntax;你当然可以使用旧的/ [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInteger:UIControlEventTouchDown], @"UIControlEventTouchDown", ...

制作字典

然后使用您的字符串,您将从字典中获得NSNumber

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

如果您没有使用新的下标语法,则可以通过NSNumber检索objectForKey:

答案 2 :(得分:0)

下面是一些简短的代码,它们描述了UIControlEvents位掩码中的位:

- (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;
}

示例输出:

[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