Jacky Shek
Jacky Shek

Reputation: 955

Cannot add UIBarButtonItem to toolbar in UINavigationController

I have tried to add the UIBarButtonItem to the toolbar of UINavigationController, but it does not show any buttons and found that the _toolbar array is empty when I am doing debug.

override func viewDidLoad() {
        var default_title = ["A","B"," C","D"]
        var items: NSArray = NSArray()
        for title in default_title
        {
            items.arrayByAddingObject(UIBarButtonItem(title: title, style: UIBarButtonItemStyle.Bordered, target: self, action: ""))
        }

        self.setToolbarItems(items as [AnyObject], animated: true)
        self.setToolbarHidden(false, animated: false)
}

Upvotes: 2

Views: 1013

Answers (1)

Jacky Shek
Jacky Shek

Reputation: 955

I have found the problem which is the items should be Array<UIBarButtonItem>.

code:

override func viewDidLoad() {
    var default_title = ["A","B"," C","D"]
        var items: Array<UIBarButtonItem> = []
        for title in default_title
        {
            items.append(UIBarButtonItem(title: title, style: UIBarButtonItemStyle.Bordered, target: self, action: ""))
        }

        self.setToolbarItems(items as [AnyObject], animated: true)
        self.setToolbarHidden(false, animated: false)
}

Upvotes: 3

Related Questions