1

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.

Jaqueline
  • 465
  • 2
  • 8
  • 25
  • why not just have the button hidden in the ViewDidLoad of the Contacts view. – Martheli Sep 09 '17 at 22:14
  • 1
    ...because when you click on the tab bar to go to your contacts that button needs to be there. I just dont want the button there when you click to get there by clicking the new message button – Jaqueline Sep 09 '17 at 23:01

1 Answers1

1

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
        }
    }
}
Shades
  • 5,568
  • 7
  • 30
  • 48
  • Where do I find the name of the segue identifier? – Jaqueline Sep 09 '17 at 22:51
  • @Jaqueline Did you create a segue from RecentsVC to Contacts in Interface Builder or do you instantiate Contacts programtacially? – Shades Sep 09 '17 at 22:54
  • I have it go to a navigation controller which is set as the root view controller to the ContactsVC – Jaqueline Sep 09 '17 at 22:58
  • @Jaqueline So if you click that circle in between the views on the storyboard and go to Attributes Inspector, there's a field for Identifier – Shades Sep 09 '17 at 22:59
  • @Jaqueline https://stackoverflow.com/questions/39904328/xcode-where-to-assign-the-segue-identifier – Shades Sep 09 '17 at 23:00
  • Yes but nothing shows up in the attributes inspector. – Jaqueline Sep 09 '17 at 23:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154046/discussion-between-shades-and-jaqueline). – Shades Sep 09 '17 at 23:07