Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 creation of the UIButton. How would I add in the timer to be sent down to the method? I've tried withObject:timer but it gives me a warning and crashes at runtime.

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(0.009) target:self selector:@selector(moveStickFig:) userInfo:stickFig repeats:YES];
[stickFig addTarget:self action:@selector(tapFig:andTime:) forControlEvents:UIControlEventTouchUpInside];

This is the method I'm sending it down to:

-(void) tapFig:(id)sender andTime:(NSTimer *)timer

I've also tried [stickFig performSelector:@selector(tapFig:andTime) withObject:nil withObject:timer] after I defined the UIButton, but that also results in a warning and crashes.

like image 891
Chris Avatar asked Jan 20 '23 15:01

Chris


1 Answers

You can't - UIControl action selectors are invoked with no parameters, the control that is the source of the action, or the control that is the source of the action and the UIEvent which occurred on that control. In IB you have to connect the UIButton to such a method: you can't add any other custom parameters.

If you want it to have access to other objects, they need to be instance variables.

Review Apple's Introduction to Objective C if you want to understand how to define instance variables.

like image 98
Phil Willoughby Avatar answered Jan 31 '23 06:01

Phil Willoughby