0

I have a bunch of UIBarButtonItems in a UIToolbar. Each of which has its SystemItem set in Storyboard, so that they'll look like system icons.

I'd rather not make an IBAction for each and every one of them, so I need some way to differentiate them in a switch statement. I am assuming the best way to do this is by looking at their SystemItem, since that's the only property that makes them unique from one another.

Here's what I've got so far:

@IBAction func buttonPressed(_ sender: UIBarButtonItem) {
    let controller = UIImagePickerController()
    controller.delegate = self

    switch sender.[WHAT DO I PUT HERE?] {
    case .compose:
        controller.sourceType = .photoLibrary
    case .camera:
        controller.sourceType = .camera
    default:
        break
    }

    present(controller, animated: true)
}

If there is a better way to differentiate between UIBarButtonItems, I'm all ears.

1 Answers1

0

You can use

switch sender.tag {
case 0 :
    controller.sourceType = .photoLibrary
case 1 :
    controller.sourceType = .camera
default:
    break
}

and set a different tag for each button

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • 1
    In case it's not obvious to others, you first need to set the tag of each button. And it's always best to avoid using a tag value of `0` since that is the default tag. – rmaddy Apr 27 '19 at 21:38