How to do screen transition programmatically with Swift?

1

Good evening, everyone. I'm trying to do screen transitions programmatically with swift, but I can not. I tried using the navigationController, but it did not work, it always gives a different error. The screens are set up in the same storeboard, I just need to go from screen 1 to screen 2. I tried to use protocols too, but I could not, and I was still more involved. Here's the code I'm trying to use to make the screen switch, this excerpt is in the ViewController:

@IBAction func btnSegue() {
    let newTela = TelaDoisViewController(delegate: self)

    if let navigation = navigationController {
        navigation.pushViewController(newTela, animated: true)
    }

}

Thanks in advance for the help.

    
asked by anonymous 06.11.2015 / 03:18

2 answers

1

If you are using NavigationController :

@IBAction func chamarNavigationController() {
   let storyBoard = UIStoryboard(name: "Main", bundle: nil)
   let novoNavigation = storyBoard.instantiateViewControllerWithIdentifier("NavViaCodigo")
   self.navigationController?.pushViewController(novoNavigation, animated: true)
}

// voltar pro navigation anterior
@IBAction func voltarProNavigationAnterior() {
   navigationController?.popViewControllerAnimated(true)
}

If you are using ViewController :

@IBAction func chamarViewController() {
  let storyBoard = UIStoryboard(name: "Main", bundle: nil)
  let novoViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewViaCodigo")
  self.presentViewController(novoViewController, animated: true, completion: nil)
}

// voltar pro viewController anterior
@IBAction func voltarPraViewAnterior() {
  self.dismissViewControllerAnimated(true, completion: nil)
}

ps.: I've done an example of how to do these transitions using Segue and programaticamente if you'd like to take a look > Code sample.

    
06.11.2015 / 18:40
1

For you to use the navigation the ViewController must be "inside" a navigation.

But if you just want to show another view you can use the following code:

let vc = ViewController() //Aqui você vai estanciar a sua ViewController de destino.
self.presentViewController(vc, animated: true, completion: nil)
    
06.11.2015 / 14:39