1

I am unable to add a tool bar to my tableView using the Xcode main.storyboard. Thus, I tried coding it in manually in the viewDidLoad()

    let logOutButton = UIBarButtonItem(title: "Log Out", style: UIBarButtonItemStyle.Bordered, target: self, action: "logOut")
    var bottomBarButtonArray = [UIBarButtonItem]()
    bottomBarButtonArray.append(logOutButton)

    self.navigationController!.setToolbarHidden(false, animated: true)

    self.navigationController!.toolbar.items = bottomBarButtonArray

May I know how do I set the logOutButton text and also how to detect if logOutButton has been pressed.

I tried logOutButton.description = "Log Out" but it does not work.

My toolbar does appear but I have no idea how to add the text for the logout button. enter image description here

Swift Everyday
  • 271
  • 1
  • 5
  • 13
  • 2
    See this : [How to add ToolBar button to Tool Bar in my Navigation Controller](http://stackoverflow.com/questions/25943951/how-to-add-toolbar-button-to-tool-bar-in-my-navigation-controller). – Bista Jan 14 '16 at 11:44
  • Agree with @ksa_coder – Bista Jan 14 '16 at 16:09
  • The solution that ksa_coder linked to me works but I really want to learn how to do it programmatically as well. I have already tried that link before asking but it does not work for me. I could add it to the top navigation bar but not the bottom one. It does not appear no matter what I did. I have been working on it for 2 days straight now and I am very new to swift. Please show me where i went wrong. I really appreciate any help. Thank you in advance. – Swift Everyday Jan 15 '16 at 05:40
  • I am away from my mac..try this : [Swift UIToolBar Append UIBarButtonItem Items](http://stackoverflow.com/questions/24715930/swift-uitoolbar-append-uibarbuttonitem-items) – Bista Jan 15 '16 at 06:41

2 Answers2

1

The solution is to use UIViewController as parent class and not UITableViewController

How to add a toolbar to a TableView in iOS

Community
  • 1
  • 1
ksa_coder
  • 1,393
  • 3
  • 15
  • 38
0

If the TableView happens to be embedded in a Navigation Controller, there is an easy solution: you can add the Toolbar straight away in the NavigationController. The checkbox 'Shows Toolbar' in the Attributes Inspector is all you need. Details: https://stackoverflow.com/a/34832688/5897915

caucus
  • 182
  • 1
  • 10