Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 848
Stephen C Avatar asked Dec 25 '22 01:12

Stephen C


1 Answers

Try like this inside prepareforsegue method.

override func prepare(for segue: UIStoryboardSegue, sender:Any){
     if let barButton = sender as? UIBarButtonItem {
          if saveButton == barButton {
               //Do what you want
          }
     }
}
like image 176
Nirav D Avatar answered Dec 26 '22 14:12

Nirav D