Swift 5 & iOS 13+ Programmatic Example
- You must mark your function with
@objc
, see below example!
- No parenthesis following after the function name! Just use
#selector(name)
.
private
or public
doesn't matter; you can use private.
Code Example
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let menuButtonImage = UIImage(systemName: "flame")
let menuButton = UIBarButtonItem(image: menuButtonImage, style: .plain, target: self, action: #selector(didTapMenuButton))
navigationItem.rightBarButtonItem = menuButton
}
@objc public func didTapMenuButton() {
print("Hello World")
}