NSMutableDictionary with UIButton* as keys - iPhone development

前端 未结 4 1615
栀梦
栀梦 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:06

    UIButtons have a description property that can be used as a dictionary key:

    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithCapacity:1];
    UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 10.0f, 10.0f)];
    id myObject;
    [myDictionary setObject:myObject forKey:myButton.description];
    
    // somewhere else in code
    id myLookedUpObject = [myDictionary objectForKey:myButton.description];
    // do something with myLookedUpObject
    

提交回复
热议问题