Objective C: Sending arguments to a method called by a UIButton

后端 未结 6 770
甜味超标
甜味超标 2021-01-06 10:26

I have a method that is being called when a UIButton is clicked. When I create the button I want it to store an NSTimer as an argument.

This is the timer and the cr

6条回答
  •  半阙折子戏
    2021-01-06 11:18

    You could take the approach where you extend UIButton.

    @interface MyButton : UIButton
    @property (nonatomic, retain) NSDictionary *userInfo;
    @end
    

    Then your method

    - (void)foo:(MyButton *)sender{
        NSLog(@"%@", [sender.userInfo valueForKeyPath:@"extraData"]);
    }
    

    And to set userInfo

    ...
    MyButton *myButton = (MyButton *)[UIButton buttonWithType:UIButtonTypeCustom];
    //set up a dictionary with info, called userInfo
    myButton.userInfo = userInfo;
    [myButton addTarget:self selector:@selector(foo:) forControlEvent:UIControlEventTouchUpInside];
    

    Would that work for you?

提交回复
热议问题