Press UIBarButtonItem programmatically in swift?

If you look at Objective-C equivalent questions, you'll see implementations that use performSelector:. In Swift, you don't have access to this method. Here's the official word on it from Apple:

The performSelector: method and related selector-invoking methods are not imported in Swift because they are inherently unsafe.

You can solve this problem by using UIApplication.sendAction(_:to:from:forEvent:) instead. Technically, you should be doing this in Objective-C too.

Let's say your UIBarButtonItem is defined as barButtonItem. The following code would do what you are asking.

UIApplication.sharedApplication().sendAction(barButtonItem.action, to: barButtonItem.target, from: self, forEvent: nil)

Swift 4 (and 3) syntax

UIApplication.shared.sendAction(barButtonItem.action!, to: barButtonItem.target, from: self, for: nil)

Works in Swift 5:

_ = button.target?.perform(button.action, with: nil)