MortalMan
MortalMan

Reputation: 2612

Access nav bar button in code

I have a button on the right side of my nav bar, it is a Done button. I created it on my storyboard. I am trying to disable it in code. How can I access this button?

Upvotes: 1

Views: 2879

Answers (4)

Aditya Koukuntla
Aditya Koukuntla

Reputation: 325

if let iuRightNavigationButton = self.navigationItem.rightBarButtonItem
{
iuRightNavigationButton.enable = false
}

It basically checks for the rightBarbutton item,if there is one then it disables.You need not declare any iboutlet to do this

Upvotes: 0

Victor Sigler
Victor Sigler

Reputation: 23459

There are two ways of do it:

  1. As you have created the button using Interface Builder, then you can declare an @IBOutlet for it using the drag-and/drop utility of Xcode and disable/hide the button in the navigation bar in the following way:

    // hide the button
    self.rightButton.hidden = true
    
    // disable the button
    self.rightButton.enabled = false
    
    // change its title
    self.rightButton.setTitle("NEW TITLE", forState: .Normal)
    

    Where the @IBOutlet is like this:

    @IBOutlet weak var rightButton: UIButton!
    
  2. In case you don't want to declare any @IBOutlet you can set the self.navigationItem.rightBarButtonItem to nil in any place you want int this way:

    // remove the button
    self.navigationItem.rightBarButtonItem = nil
    
    // disable the button
    self.navigationItem.rightBarButtonItem?.enabled = false
    

    And the above code disable any previous button you have set.

I hope this help you.

Upvotes: 4

Pieter
Pieter

Reputation: 21

try to create outlet in your view controller: @IBOutlet weak var doneButton : UIBarButtonItem! link the barbuttonItem to your doneButton variable in the storyboard.

assigned the value false when to disable it. At view did load:

doneButton.enabled = false

Upvotes: 0

Shades
Shades

Reputation: 5616

navigationItem.rightBarButtonItem?

Upvotes: 0

Related Questions