Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift UIToolBar Append UIBarButtonItem Items

Tags:

ios

swift

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.

like image 328
Matt Long Avatar asked Jul 30 '26 08:07

Matt Long


1 Answers

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
like image 181
Nate Cook Avatar answered Aug 02 '26 00:08

Nate Cook