I am a beginner of iOS and in the programming tutorials when adding button programmatically, target is always set to self, and in that controller where it is being created, an action (IBAction) is written. Or through the interface builder, target is set to File’s Owner.
My question is that in what scenario (an example would be great) that target is not self.
One scenario I can think of is when in a class’s (which is not a ViewController) method a button is created depending on a condition, since that class is not a ViewController, when initializing an object of that class, a reference to the current ViewController would be set and that will be used as target where action is defined for the button if it appears.
You can direct the selector to any target – the reason why ordinarily,
selfis the target is because it is quite normal to instantiate a UIButton/UIBarButtonItem in code, so a lot of tutorials include the implementation the selector references in the same class.You could, for example, make a class that when instantiated in your View Controller is designed to only handle these button invocation actions:
IBAction lets you tell Interface Builder that the method implementation can be connected through your xib/storyboard.