Usually you will see the goal and action at the same time.
Purpose and action are used to indicate a specific method. In the code snippet, you create a UIBarButtonItem . UIBarButtonItem should know which method it should call when it is used.
How do you tell which method to call?
"Just pass the method reference," you can say:
let rightButton = UIBarButtonItem( title: "Done", style: .done, methodToCall: self.myMethod)
Unfortunately, this only works fast. UIBarButtonItem is an object C API, so this approach cannot be used.
In a C object, Selector represent methods, but they do not store which object calls the method. This is why we need an additional target parameter. It indicates to which object the method should be called. On the other hand, action indicates which method to call.
Here we want to call self.myMethod . The object on which the method is called is self , and the called method is myMethod . Big! Now let it go!
let rightButton = UIBarButtonItem( title: "Done", style: .done, target: self, action:
Sweeper
source share