What's the best way to call an IBAction from with-in the code?

2 Solutions Collect From Internet About “What's the best way to call an IBAction from with-in the code?”

The proper way is either:

- [self functionToBeCalled:nil] 

To pass a nil sender, indicating that it wasn’t called through the usual framework.


- [self functionToBeCalled:self]

To pass yourself as the sender, which is also correct.

Which one to chose depends on what exactly the function does, and what it expects the sender to be.

IMO calling an IBAction should be triggered by UI actions only (e.g. a button tap). If you need to run some code from multiple places, then you can extract that code from the IBAction into a new method, and call that method from both places

- (IBAction)onButtonTap:(id)sender
    [self doSomething];

This allows you to do extra logic based on the sender (perhaps you might assign the same action to multiple buttons and decide what to do based on the sender parameter). And also reduces the amount of code that you need to write in the IBAction (which keeps your controller implementation clean).