NSMutableDictionary with UIButton* as keys - iPhone development

前端 未结 4 1570
栀梦
栀梦 2021-02-20 12:38

I\'m new to iPhone development and I have a question that may have a very simple answer. I am trying to add buttons to a view and these buttons are associated with a custom clas

4条回答
  •  悲&欢浪女
    2021-02-20 13:17

    One way I found was to use construct an NSValue to use as the key. To create the that use:

    [NSValue valueWithNonretainedObject:myButton].

    The caveat here seems to be that if the button is garbage collected, the key will hold an invalid reference.

    You can get the reference to the UIButton again while looping through the Dictionary like so:

    for (NSValue* nsv in myDict) {
        UIButton* b = (UIButton*)[nsv nonretainedObjectValue];
        ...
    }
    

提交回复
热议问题