I have been using
sendAction
nil
target
Cells(UITableViewCell/UICollectionViewCell)
ViewControllers
#selector
selectorname
classname
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.