Passing custom data in [UIButton addTarget]

后端 未结 3 704
暖寄归人
暖寄归人 2020-12-08 07:54

How do I add custom data while specifying a target in a UIButton?

id data = getSomeData();
[button addTarget:self 
           action:@selector(b         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-08 08:23

    You can't really do that. What you can do is put the data in a dictionary and use the button to get it later.

    E.g.

    myDataDict = [NSDictionary dictionaryWithObjectsAndKeys:someData, button, nil];
    

    Then later;

    -(void) buttonPress:(id)sender
    {
      data = [dataDict objectForKey:sender];
    }
    

    If your buttons are specified in InterfaceBuilder you can use the 'tag' property of a button to lookup the data, although you will need to convert it to an NSNumber for use with the dictionary.

提交回复
热议问题