Search code examples
iosswiftuitableviewnavigationbartabbarcontroller

swift NavigationViewController. UIBarButtonItem does not appear


I'm trying to solve the following issue for hours and it seems that I don't understand a vital point, because all solutions I tested do not work.

At first my current storyboard: storyboard

I want to add an add-button in the navigationbar of "Workouts"-tableview, but I don't know how, because code that works in the "Exercises"-tableview is not working.

Here you can see the viewDidLoad() function of "Exercises"-tableview:

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = UIColor(red: 110.0/255.0, green: 110.0/255.0, blue: 110.0/255.0, alpha: 1.0)
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: Selector("addItem"))

   // addDefaultExercises()
    showAtStart()
}

The Workouts-tableview code is nearly the same:

        override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.view.backgroundColor = UIColor(red: 110.0/255.0, green: 110.0/255.0, blue: 110.0/255.0, alpha: 1.0)

        self.title = "Your Title"     
self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "barButtonItemClicked:"), animated: true)

        addDefaultPhrases()

    }

Both classes inherit from UITableViewController. Since the other frontpage of the TabViewController also does not show the button, maybe the TabBarController produce the issue.


Solution

  • I would suggest having the UITabBarController as the root & initial view controller.

    Then the create a relationship from the UITabBarController to the UINavigationController and then from the UINavigationController to the workoutsViewController. Then repeat again for the trainingViewController.

    Add a bar button item in the storyboard for the navigation bar, and change the button in the identifier. Do this for both ViewControllers.