iosswiftuikituibarbuttonitem

How to determine which UIBarButtomSytemItem has been pressed


in a navigation controller i have 2 bar button items linked in the StoryBoard: 1 on the right with System Item = Add and 1 on the left with System Item = Cancel. Both buttons are linked to the same action. how can i determine which one has been triggered using a switch statement?

@IBAction func pressedBarButtonItem(sender: UIBarButtonItem) {
    switch sender {
    case UIBarButtonSystemItem.Add:
      print("UIBarButtonSystemItem.Add button has been pressed ...")
    default:
      break
    }
}

this causes an error "Enum case "Add" is not a member of type 'UIBarButtonItem'" so is there an Enum attribute to the bar button that says it is a system type?

thanks


Solution

  • UIBarButtonSystemItem is used only at initialization to define a system image, those are not styles or types and are not stored or affected to the button.

    You may want to use tags or outlet references to select the right action, or use different IBActions, which seems more appropriate.