Stephen C Stephen C - 2 months ago 63
Swift Question

compare sender: Any? to UIBarButtonItem

I am new to swift, and just happen to have Swift 3 released, but apple document still using Swift2 syntax.

Understand that below (Swift2)

@IBOutlet var weak saveButton:UIBarButtonItem!


This method lets you configure a
ViewController
before it's presented.

override func prepareForSegue(segue: UIStoryboardSegue, sender:AnyObject?) {
if saveButton === sender { //originally swift 2 can compare

}


Has changed to (Swift3)

override func prepare(for segue: UIStoryboardSegue, sender:Any){
}


Now on swift 3 I cannot check
if sender = saveButton


I am very new to Swift, is that I need to cast it like
sender as UIBarButtonItem?


Original Apple Guide

Answer

Try like this inside prepareforsegue method.

override func prepare(for segue: UIStoryboardSegue, sender:Any){
     if let barButton = sender as? UIBarButtonItem {
          if sender == barButton {
               //Do what you want
          }
     }
}
Comments