Access from one UIViewController to another UIViewController without segues

0

I have a UIViewController called AController . This displays a second UIViewController called BController , but does so in modal format - overCurrentContext .

BController has 3 buttons, each one leads to a UIViewController different, each of these 3 UIViewController has a UINavigationViewController .

The problem I have is that I do not know how to access from BController to the other UIViewController without losing navigation or TopBarLayout .

My code is this:

AController

Alamofire.request(urlString, method: .post, parameters: parametros).responseJSON { response in
    if let result = response.data {
        do {
            let json = try JSON(data: result)
            if json["resultado"] == "true" {
                let modalViewController = BController()
                modalViewController.modalPresentationStyle = .overCurrentContext
                self.present(modalViewController, animated: true, completion: nil)
            }
            else {
                self.mostrarAlerta(titulo: "Error", mensaje: "")
            }
        }
        catch {
            print("Error: \(error)")
            self.mostrarAlerta(titulo: "Error", mensaje: "")
        }
    }
}

BController

import UIKit

class BController: UIViewController {

    var viewPrincipal = UIView()

    var btnAccion1 = UIButton()
    var btnAccion2 = UIButton()
    var btnAccion3 = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()

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

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


        btnAccion1 = UIButton(frame: CGRect(x: 0, y: 0, width: width - 100, height: 50))
        btnAccion1.setTitle("Accion 1", for: .normal)
        btnAccion1.addTarget(self, action: #selector(accion1(_:)), for: .touchUpInside)

        btnAccion2 = UIButton(frame: CGRect(x: 0, y: 0, width: width - 100, height: 50))
        btnAccion2.setTitle("Accion 2", for: .normal)
        btnAccion2.addTarget(self, action: #selector(accion2(_:)), for: .touchUpInside)

        btnAccion3 = UIButton(frame: CGRect(x: 0, y: 0, width: width - 100, height: 50))
        btnAccion3.setTitle("Accion 3", for: .normal)
        btnAccion3.addTarget(self, action: #selector(accion3(_:)), for: .touchUpInside)


        viewPrincipal.addSubview(btnAccion1)
        viewPrincipal.addSubview(btnAccion2)
        viewPrincipal.addSubview(btnAccion3)


        view.addSubview(viewPrincipal)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @objc func accion1(_ sender: UIButton) {
        if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Controller1") as? Controller1 {

            if let navigator = navigationController {
                navigator.pushViewController(viewController, animated: true)
            }
            else {
                print("Error 2")
                if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavegacionController1") as? NavegacionController1 {
                    present(vc, animated: true, completion: nil)
                }
            }
        }
        else {
            print("Error 1")
        }
    }

    @objc func accion2(_ sender: UIButton) {
    }

    @objc func accion3(_ sender: UIButton) {
    }

    @objc func cerrarVentana(_ sender: UIButton) {
        print("Cerrar ventana")
        self.dismiss(animated: true, completion: nil)
        self.presentedViewController?.dismiss(animated: true, completion: nil)
    }
}
    
asked by Alberto Mier 30.01.2018 в 13:01
source

1 answer

0

You should instantiate The StoryBoard. For example, I use it this way:

if let newController = UIStoryboard(name: "DetallesVuelosView", bundle: Bundle.main).instantiateInitialViewController() as? DetallesVuelosViewController {
                                                        if let controller = self.controller as? VuelosViewController {

                                                         controller.navigationController?.pushViewController(newController, animated: true)

                                                        }
                                                    }
    
answered by 14.06.2018 в 16:21