sarah
sarah

Reputation: 1221

Why setting the barItems is not working?

I have a UIViewController, and I embed it into a UINavigationController.

I want to show one item in the toolbar (and by toolbar, I mean this:

This is my code in viewDidLoad method

self.navigationController?.toolbarHidden = false
self.navigationController?.toolbar.items?.append(UIBarButtonItem(title: "Buy Potato", style: .Plain, target: self, action: #selector(ViewController.buyPotato)))
self.navigationController?.toolbarItems?.append(UIBarButtonItem(title: "Buy Potato", style: .Plain, target: self, action: #selector(ViewController.buyPotato)))     
self.toolbarItems?.append(UIBarButtonItem(title: "Buy Potato", style: .Plain, target: self, action: #selector(ViewController.buyPotato)))

and I already have the method buyPotato

func buyPotato() {

    }

as you see, I tried to do that using either the viewController or the navigationController, but it doesn't work.

All I can see is the toolbar at the bottom of my screen but without any button.

Upvotes: 3

Views: 992

Answers (2)

brycejl
brycejl

Reputation: 1491

Delete

self.setToolbarItems(barButtonItems, animated: true)

Add

self.toolbarItems = barButtonItems

Upvotes: 1

amagain
amagain

Reputation: 2072

self.navigationController?.toolbarHidden = false
var items = [UIBarButtonItem]()
items.append(
UIBarButtonItem(barButtonSystemItem: .Plain, target: self, action: nil))
items.append(
UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onClickedToolbeltButton:"))
self.setToolbarItems(barButtonItems, animated: true)

This has to work for you as per the answer written here.

Upvotes: 4

Related Questions