Alex Donner Alex Donner - 5 months ago 35
Swift Question

Swift Error/Crash when setting NavigationButton

Does anybody know why my code Crashed/doesn't work?


Answer Source

You need to declare your UIBarbuttonItem in your viewDidLoad. Self won´t be recognised otherwise.

override func viewDidLoad() {
    let rightBarButton = UIBarButtonItem(title: "Edit", style: .done, target: self, action: #selector(buttonClicked(_:)))
    self.navigationItem.rightBarButtonItem = rightBarButton

func buttonClicked(_ button: UIBarButtonItem) { ... }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download