AUK4SO AUK4SO - 5 months ago 22
Swift Question

Sending anonymous actions using UIApplication.sharedApplication().sendAction in Swift 3

I have been using

method with
to communicate actions from
, instead of implementing a delegate.

Since Swift 2.2 the syntax for selector has been updated and I get some warning. The new
syntax insists on specifying the
followed by
. If I mention the class name then there is no meaning in setting the target to nil.

Any workaround for this?

class RedeemCell: UICollectionViewCell {
@IBAction func redeemAction(sender: AnyObject) {
UIApplication.sharedApplication().sendAction("updateCartWithTotalAmountPayableWithDiscount:", to: nil, from: self, forEvent: nil)

class CartVC: UIViewController {

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell: UICollectionViewCell?

cell = collectionView.dequeueReusableCellWithReuseIdentifier("redeempoints", forIndexPath: indexPath)

return cell!;

func updateCartWithTotalAmountPayableWithDiscount(sender: AnyObject) {
print("this will be called as the action movies through responderchain and encounters this method");


You can (and probably should) pass #selector(CartVC.updateCartWithTotalAmountPayableWithDiscount). When you do this, you're saying that you want a selector for the updateCart... function that the CartVC class defines — as opposed to the updateCart... function defined by some other class.

This is different from the meaning of passing nil as your target — the class you name in the #selector expression clarifies what the message you want to send is, based on the class that defines it. The target is about who you send the message to. When you choose nil for the target, you're saying the message should go to the first object that can handle it — which could be one of several instances of CartVC, or an instance of another class that accepts the same message.