Swift UIToolBar Append UIBarButtonItem Items

ios, swift

Solution

I was able to add the toolbar's items by force unwrapping the array. It seems like that shouldn't be necessary, since the `items` property is implicitly unwrapped, but that made the compiler let it through.

I'm not sure why your `items += displayModeButton` line isn't working -- are you sure it's being called? This code adds a button with the title "Another" to my toolbar:

var items = [AnyObject]()

items += self.barButton.items!
items += UIBarButtonItem(title: "Another", style: .Plain, target: nil, action: "")
self.barButton.items = items

Problem

I’m not getting the syntax right. Anybody know what's missing here? I’m using Xcode 6 beta 3 and I'm trying to dynamically add a toolbar item to my (manually added) toolbar in my viewDidLoad. There are two issues. First, I can't append the items in my toolbar to an array. Second, while the compiler allows my call to append (+=), when it finishes, the array is still empty. ``` override func viewDidLoad() { super.viewDidLoad() var items = [AnyObject]() // Zero items, mutable, right? // items += buttonBar.items // Not allowed --> compiler error if let displayModeButton = self.splitViewController.displayModeButtonItem() { items += displayModeButton // Still zero items after append } buttonBar.items = items // Still zero items after append } ``` buttonBar is an IBOutlet that is set in IB.

Original source