Whenever I create an IBAction I always have to set the default Type (sender) from AnyObject to the only other option which is the actual class type.
Is there a reason for it to always default to AnyObject rather than the object I have explicitly added?
I believe I understand the difference between a UIObject and AnyObject but I fail to see why AnyObject should be the default.
One reason I can think of is let's say I write the interface of an object first but I don't know yet what control type will invoke the action. Or further more I end up with more than one type of control.
By defaulting to AnyObject it fits nicely with objc id type. Also it is scaleable and will work for the general case.
It's also part of Target-Action whereby only certain method signatures must be implemented for it to work.
This extract gives a good scenario -
The senderparameter usually identifies the control sending the action message (although it can be another object substituted by the actual sender). The idea behind this is similar to a return address on a postcard. The target can query the sender for more information if it needs to. If the actual sending object substitutes another object as sender, you should treat that object in the same way. For example, say you have a text field and when the user enters text, the action method nameEntered: is invoked in the target:
- (void)nameEntered:(id) sender {
NSString *name = [sender stringValue];
if (![name isEqualToString:@""]) {
NSMutableArray *names = [self nameList];
[names addObject:name];
[sender setStringValue:@""];
}
}
Here the responding method extracts the contents of the text field, adds the string to an array cached as an instance variable, and clears the field. Other possible queries to the sender would be asking an NSMatrix object for its selected row ([sender selectedRow]), asking an NSButton object for its state ([sender state]), and asking any cell associated with a control for its tag ([[sender cell] tag]), a tag being a numeric identifier
It makes sense to default to id / AnyObject as you can always change it to ensure better type safety.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With