title: How to add a UIBarButtonItem programmatically to UINavigationBar dragged onto view
body: <p>I have searched and looked at multiple solutions to this problem but can't seem to have anything work for me. Here is my code and a screenshot. If it makes any difference I manually dragged the navigation bar onto the view and connected an outlet.Thanks. I appreciate any help on this.<img src="https://i.stack.imgur.com/hHme1.png" alt="enter image description here"></p>
  
  <pre><code>class SubjectsViewController: UIViewController {
  
      @IBOutlet var myTableView: UITableView!
  
      @IBOutlet var noBooksLabel: UILabel!
  
      @IBOutlet var navigationBarTitle: UINavigationBar!
  
      func backAction() -&gt; Void {
  
          self.navigationController?.popViewControllerAnimated(true)
  
      }
  
      override func viewWillAppear(animated: Bool) {
  
          self.noBooksLabel.hidden = true
  
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          descriptions.removeAll(keepCapacity: true)
          usernames.removeAll(keepCapacity: true)
          imageFiles.removeAll(keepCapacity: true)
  
          println("Subject view controller")
  
          self.navigationBarTitle.topItem?.title = "\(selectedSubject)"
  
          let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")
  
          self.navigationItem.leftBarButtonItem = backButton
  
  
          pickedSubject = selectedSubject
  
          println("Picked Subject = \(pickedSubject)")
  
          println("Subject view controller")
  
          self.navigationBarTitle.topItem?.title = "\(selectedSubject)"
  
          //let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")
          //self.navigationItem.leftBarButtonItem=barBtn;
  
          var subjectQuery = PFQuery(className: "BookPosting")
  
          subjectQuery.whereKey("CourseSubject", equalTo: pickedSubject)
          subjectQuery.findObjectsInBackgroundWithBlock {
  
              (objects: [AnyObject]!, error: NSError!) -&gt; Void in
  
              if error == nil {
  
                  // The find succeeded.
                  println("Successfully retrieved \(objects.count) scores.")
  
                  // Do something with the found objects
                  if let objects = objects as? [PFObject] {
  
                      for object in objects {
  
                          descriptions.append(object["Description"] as String)
  
                          usernames.append(object["username"] as String)
  
                          imageFiles.append(object["imageFile"] as PFFile)
  
                          self.myTableView.reloadData()
  
                      }
  
                  }
  
                  println("descriptions.count = \(descriptions.count)")
  
  
  
                  if (descriptions.count == 0) {
  
                      self.noBooksLabel.hidden = false
  
                  } else {
  
                      self.noBooksLabel.hidden = true
  
                  }
  
              } else {
  
                  // Log details of the failure
                  println("Error: \(error) \(error.userInfo!)")
  
              }
  
          }
  
      }
  
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
          return descriptions.count
  
      }
  
      func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat {
  
          return 100
  
      }
  
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
          var cell: bookCell = tableView.dequeueReusableCellWithIdentifier("booksCells") as bookCell
  
  
          cell.subjectBookDescription.text = descriptions[indexPath.row]
          cell.subjectBookPosterUsername.text = usernames[indexPath.row]
  
          imageFiles[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData!, error: NSError!) -&gt; Void in
  
              if error == nil {
  
                  let image = UIImage(data: imageData)
  
                  cell.subjectBookImage.image = image
  
              }
  
          }
  
          return cell
      }
  
  
  
  
  }
  </code></pre>