Home > OS >  How to navigate from one View Controller to the other?
How to navigate from one View Controller to the other?

Time:10-20

I want to navigate from one View Controller to another.

  let vc = SecondViewController()

I have tried until now :

  vc.modalPresentationController = .fullScreen
  self.present(vc, animated: true) //self refers to the main view controller 

Im trying to open a new ViewController when the users manages to register or to log in.I am new to software developing, and I want to ask, is this the best method to navigate from one ViewController to another, im asking because as I can see the mainViewController is not deinit(). I have found other similar questions and tried the answers, the problem is with the:

       self.navigationController?.pushViewController

it doesn't work because I don't have any storyboard. The question is it is right to navigate as explained above? Thanks,

CodePudding user response:

Typically when you are doing login you would use neither push or present. There are multiple ways of handling this, but the easiest is to embed in some parent (root) VC. Here is an example:

class ViewController: UIViewController {
    private var embeddedViewController: UIViewController! {
        didSet {
            // https://developer.apple.com/documentation/uikit/view_controllers/creating_a_custom_container_view_controller
            // Add the view controller to the container.
            addChild(embeddedViewController)
            view.addSubview(embeddedViewController.view)

            // Create and activate the constraints for the child’s view.
            embeddedViewController.view.translatesAutoresizingMaskIntoConstraints = false
            embeddedViewController.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
            embeddedViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
            embeddedViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
            embeddedViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

            // Notify the child view controller that the move is complete.
            embeddedViewController.didMove(toParent: self)
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        let loginVC = LoginViewController()
        loginVC.delegate = self
        embeddedViewController = loginVC
    }
}

extension ViewController: LoginDelegate {
    func didLogin() {
        embeddedViewController = MainViewController()
    }
}

protocol LoginDelegate: AnyObject {
    func didLogin()
}

class LoginViewController: UIViewController {
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setTitle("Login", for: .normal)
        button.addTarget(self, action: #selector(didTapLoginButton), for: .touchUpInside)
        return button
    }()
    
    weak var delegate: LoginDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(loginButton)
        view.backgroundColor = .red
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        loginButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    }
    
    @objc private func didTapLoginButton() {
        delegate?.didLogin()
    }
}

class MainViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .blue
    }
}
  • Related