Sender Parameter

I have a method that accepts a sender and is called using UIbutton. How to get this object to be added to UIImageView? Basically, how can I reuse the code for a button.

- (IBAction)startClick:(id)sender { button.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"Pic_1.png"], [UIImage imageNamed:@"Pic_2.png"], [UIImage imageNamed:@"Pic_3.png"], nil]; [button setAnimationRepeatCount:1]; button.animationDuration = 1; [button startAnimating]; } 
+2
source share
4 answers

My first answer is that this is probably a mistake. UIButton and UIImageView are different subclasses of UIView (and UIButton is a subclass of UIControl ), so the only thing they have is UIView methods. If the sender indeed a UIButton , treating it as a UIImageView can lead to errors such as "unrecognized selector sent to instance", etc.

In general, a cleaner solution is to create a separate method, which is an action for UIImageView . (Unlike Java, Objective-C and Cocoa prefer not to encode the UI response code into a single method based on the type of event - rather, you logically order the code based on the operation that should happen.)

If you must call this method with both buttons and images, you can separate the logic as follows:

 - (IBAction) startClick:(id)sender { if ([sender isKindOfClass:UIButton]) { ... } else if ([sender isKindOfClass:UIImageView]) { ... } } 
+3
source

It seems, I think, you would do it.

 UIButton *button = (UIButton*) id; // then do all you button code here. 

That should work. Otherwise, it would be better to change the function to

  (IBAction)startClick:(UIButton *)theButton 

So you want to click a button.

+1
source

Do you have a socket for your button? Is the button the only user interface element that will send -startClick :? If so, write the action method as follows:

 - (IBAction)startClick:(id)sender { NSParameterAssert(sender == self.startClickButton); if (sender == self.startClickButton) { UIButton *button = self.startClickButton; [...] } } 

If you do not have an exit / property button for the button, you can test the sender class and, if necessary, perform the cast.

 - (IBAction)startClick:(id)sender { NSParameterAssert([sender isKindOfClass: [UIButton class]]); if ([sender isKindOfClass: [UIButton class]]) { UIButton *button = (UIButton *)sender; [...] } } 

Finally, it is permissible (at compile time) to send any message to the value entered by the identifier. (At run time, you will get an exception if the recipient does not respond to this message.) However, you cannot use the dot syntax.

So, if you know that the sender is always a button, you can write your method without castings. You will need to use message sending syntax instead of dot syntax.

 [button setAnimationImages: [NSArray ...]]; [button setAnimationDuration: 1]; 

It is important to understand that the syntax of a point is equivalent to the syntax of sending a message - it is not mystical.

+1
source

You can simply use the following to get the button shortcut and determine which one was clicked:

NSLog (@ "Clicked button:% @", [[sender titleLabel] text]);

Hope this helps.

Suresh

0
source

All Articles