Question

Documentation says that method signature must be the following:

- (IBAction)doSomething:(id)sender;

But I can use other signature for action, like:

- (IBAction)doSomething:(UIButton *)button; // could write sender instead of
                                            // button, but result is the same

It works, but

  • Xcode doesn't redirect if I cmd+click on selector
  • This violates convention.

Is there any other reason why it's a bad practice to add type to sender in method signature?

Was it helpful?

Solution

Both of the statements work fine.

I suspect cmd+click is not redirecting you because you are cmd+clicking on the arguments in the method name. The cmd+click is erratic when clicking on the arguments. I suggest you stick to clicking on the method name and it should work fine. I found the same thing apply if you are getting documentation (option + click).

Again, it is fine to be more specific than id with the sender. This is actually an option if you are using the graphic method to connect a button for example to your code: You can select id or UIButton.

The name of the argument here "button" is irrelevant. You can name it MountKilimanjaro and it will be fine since that argument/variable name is local to the method.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top