Error navigating between two controllers with segues in IOS

3

I have two views, A and B, each with its respective driver.

From view A I can go to B. And from the B I open a new controller that has no associated view, but I create it programmatically.

From the new controller created I have a button that when clicking, closes the new window and I use a segue to navigate from view B to view A.

The problem is that I get this error:

  

libc ++ abi.dylib: terminating with uncaught exception of type NSException   
(lldb)

The code of view B

import UIKit
import Alamofire
import SwiftyJSON

class ControladorVistaB: UIViewController {

    @IBOutlet weak var btnCrear: UIButton!
    var origen: String = "vistaA"

    override func viewDidLoad() {
        super.viewDidLoad()

        btnCrear.setTitle(" Crear ", for: .normal)
        btnCrear.titleLabel?.textColor = UIColor.white
        btnCrear.titleLabel?.font = UIFont.boldSystemFont(ofSize: 25)
        btnCrear.titleLabel?.adjustsFontSizeToFitWidth = true
        btnCrear.setTitleColor(UIColor.white, for: .normal)
        btnCrear.addTarget(self, action: #selector(ventanaModal(_:)), for: .touchUpInside)

        self.view.backgroundColor = AppDelegate().hexStringToUIColor("#E6F4E8")
    }

    func ventanaModal(_ sender: UIButton) {
        self.abrirModalRegistro()
    }

    func navegarDestino(destino: String) {
        print("El destino es: \(destino)")
        self.performSegue(withIdentifier: destino, sender: self)
    }

    func abrirModalRegistro() {
        let modalViewController = ControladorC()
        modalViewController.modalPresentationStyle = .overCurrentContext
        modalViewController.destino = origen
        present(modalViewController, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Controller code C:

import UIKit
import SnapKit

class ControladorC: UIViewController {

    var viewPrincipal = UIView()
    var boton = UIButton()

    var nombre: String = ""
    var destino: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        print("El destino es: \(destino)")

        let tamPantalla = UIScreen.main.bounds.size
        let width = tamPantalla.width
        let height = tamPantalla.height

        viewPrincipal = UIView(frame: CGRect(x: 15, y: 70, width: width - 30, height: height - 90))
        viewPrincipal.backgroundColor = UIColor.white
        viewPrincipal.layer.borderColor = UIColor.black.cgColor
        viewPrincipal.layer.borderWidth = 2

        boton.setTitle(" Entendido ", for: .normal)
        boton.setTitleColor(UIColor.white, for: .normal)
        boton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
        boton.titleLabel?.adjustsFontSizeToFitWidth = true
        boton.addTarget(self, action: #selector(navegarDestino(_:)), for: .touchUpInside)

        viewPrincipal.addSubview(boton)

        boton.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(viewPrincipal).offset(20)
            make.right.equalTo(viewPrincipal).offset(-20)
            make.bottom.equalTo(viewPrincipal).offset(-20)
            make.height.equalTo(50)
        }


        view.addSubview(viewPrincipal)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func navegarDestino(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
        self.presentedViewController?.dismiss(animated: true, completion: nil)
        ControladorVistaB().navegarDestino(destino: destino)
    }
}
    
asked by 23.11.2016 в 12:04
source

1 answer

1

As I understand you want to go from VistaA - > VistaB - > VistaC and from this last one close C and B, returning to VistaA.

Try removing your VistaB instead of calling with a segue

on your ControllerB

func navegarDestino(destino: String) {
    //print("El destino es: \(destino)")
    //self.performSegue(withIdentifier: destino, sender: self)
    self.dismiss(animated:true, completion:nil)
}

If that does not work for you, try a small delay before executing the dismiss.

func navegarDestino(destino: String) {
    //print("El destino es: \(destino)")
    //self.performSegue(withIdentifier: destino, sender: self)
    let delay = DispatchTime.now() + 1 //1 segundo después
    DispatchQueue.main.asyncAfter(deadline: delay) {
        self.dismiss(animated:true, completion:nil)
    }
}

I hope it works for you!

    
answered by 28.11.2016 в 21:54