Open Viewcontroller iOS

Asked

Viewed 432 times

0

How do I make the function below (when a navigation bar item is clicked) open another screen already created on Main.storyboard?

@objc func infoTapped (sender:UIButton) {

}
  • If you want to add the navigation bar to any VC just select it in the Interface Builder and then click in the Editor > Embed in > Navigation Controller. It is not necessary for you to be coming from one that already has the navigation bar.

1 answer

4


Has two forms.

One of them is to create a following in the storyboard, assign it an identifier and in this method call the function performSegue(withIdentifier: IDENTIFICADOR, sender: self).

The other is you instantiate the VC you want to call in this method, more or less like this:

if let destinationVC = storyboard?.instantiateViewController(withIdentifier: "IDENTIFICADOR_VC") {
        present(destinationVC, animated: true, completion: nil)
    }
  • Hello, it worked!!! I used the second option. However when my VC opens the navigation bar does not appear as well as the back button. How could I fix this?

  • 1

    To appear a navigation bar it is necessary that your source VC is a Uinavigationviewcontroller.

  • 1

    Being it navigation, instead of calling the "present", you give a self.navigationController?. pushViewController(destinationVC, Animated:true)

  • Cool, it worked too!! but now the new VC appears with the bar and the buttons that the bar contains there on the home screen, have how I take them (making them appear only on the home screen bar)?

  • Yes. In the viewWillAppear method, it makes self.navigationController.setNavigationBarHidden for true

  • I tried this: override func viewDidAppear(_ Animated: Bool){ self.navigationController?. setNavigationBarHidden = true } But says setNavigation... is method

  • 1

    Tiago, honestly, I find it interesting for you to start looking at the documentation and use the tips of Xcode itself. The signature of the "setNavigationBarHidden" method is self.navigationController. setNavigationBarHidden(Hidden: Bool, Animated: Bool), so the way to set it to true is: self.navigationController?. setNavigationBarHidden(true, Animated: true) (Animated doesn’t matter if it will be true or false). I think it’s best for you to study the documentation or you’ll always need to resort to questions and ready answers. ;)

  • Thanks for the tip, I agree with you, but this problem that seems to be silly I have tried in numerous ways and does not work.. I had already tried this code you gave me, but the whole bar disappears, including the back button. But thank you very much, it helped me a lot! I’ll try the documentation again! ;)

  • This code really takes out the entire bar and buttons. As the back button is part of the bar, if you add the bar, the buttons go together.

  • To leave the original screen without bar and the target screen with the bar, you use this code setting for Hidden on the original screen and setting to not Hidden (false) on the target screen

  • By the way, you said you’re starting in Swift. If you speak English, there are several courses that cost talk 30 reais in udemy and help a lot!

  • Thanks again for the help, but I’ve already solved this problem... I’ve bought some courses there, I’m doing!

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.