Jaqueline
Jaqueline

Reputation: 485

How to hide a button from another class

So I have a view that has a navBar with two buttons. I was wondering if it was possible if from another class I could choose wether I want those buttons to show? What I mean by this is, when you are in the RecentsVC and you click to send a new message, I have it to take you to a view called Contacts. And that view has two buttons, one of which I would like hidden. So within the IBAction for clicking to send a new message, I would like to set the property to make one of the buttons hidden.

Upvotes: 1

Views: 756

Answers (1)

Shades
Shades

Reputation: 5616

Have a Boolean variable in Contacts and set the value of that variable in the prepare(for segue: ) method of the RecentsVC class. Then use the value of that Boolean to test if Contacts should hide the nav bar button item.

class RecentsVC: UIViewController {        

    override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
        if(segue.identifier == "sendMessage") {   // If there's only one segue from this view controller, you can remove this line
            let vc = segue.destination as! Contacts
            vc.buttonIsHidden = true
        }   // If you removed the if, don't forget to remove this, too
    }
}

class Contacts: UIViewController {

    var buttonIsHidden: Bool?

    override func viewDidLoad() {
        super.viewDidLoad()

        if buttonIsHidden == true {
            self.navigationItem.leftBarButtonItem = nil
        }
    }
}

Upvotes: 1

Related Questions