Enrique Lopez
Enrique Lopez

Reputation: 17

ViewController dismiss swift ios

Hello my english is not very good, I apologize in advance.

I have a problem with my application. The scenario is as follows I have my rootViewController assigned to my ViewController controller which is my start. I have other screens that are a description screen where I have two login and registration buttons which when preloaded bring me to their controller.

Now, when I am on the log full screen form and I send the dismiss order:

ViewController registration

self.dismiss(animated: false, completion: nil)

And all ok the view is hidden but when entering the previous screen that was the description I have a validation if there is already a user if there is the dismiss order:

ViewController Description App

self.dismiss(animated: false, completion: nil)

But it does not perform the action.

Code

UIViewController

class ViewController: UIViewController {

    override func viewDidLoad() {

        FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
            if user == nil {
                let descriptionController = DescriptionController()
                present(descriptionController, animated: true, completion: nil)
            }

        }
    }
}

DescriptionController

class DescriptionController: UIViewController {

    @IBOutlet weak var sign_in_custom: UIButton!

    override func viewDidLoad() {

        FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
            if user != nil {
               self.dismiss(animated: false, completion: nil)
            }

        }

        sign_in_custom.addTarget(self, action: #selector(changeToSingIn), for: [.touchUpInside])
    }

    func changeToSingIn() {
        let singInController = SingInController()
        present(singInController, animated: true, completion: nil)
    }
}

SingInController

class SingInController: UIViewController {
    @IBOutlet weak var sign_in_custom: UIButton!
    override func viewDidLoad() {
        sign_in_custom.addTarget(self, action: #selector(singIn), for: [.touchUpInside])
    }
    func showLoad() {
        let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
        alert.view.tintColor = UIColor.black
        let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(10, 5, 50, 50) ) as UIActivityIndicatorView
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        loadingIndicator.startAnimating();
        alert.view.addSubview(loadingIndicator)
        present(alert, animated: true, completion: nil)
    }
    func hideLoad() {
        self.dismiss(animated: false, completion: nil)
    }
    func singIn() {
        if (emailVarification()){
            if (passwordVarification()){
                showLoad()
                guard let email = emailTextField.text else { return }
                guard let password = passwordTextField.text else { return }
                FIRAuth.auth()?.createUser(withEmail: email, password: password) { (user, error) in
                    hideLoad()
                    if (user != nil) {
                        self.dismiss(animated: false, completion: nil)
                    } else {
                        let alert = UIAlertController(title: "Error", message: "This is a error", preferredStyle: UIAlertControllerStyle.alert)
                        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
                        self.present(alert, animated: true, completion: nil)
                    }
                }
            } else {
                let alert = UIAlertController(title: "Error", message: "This is a error", preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
                self.present(alert, animated: true, completion: nil)
            }
        } else {
            let alert = UIAlertController(title: "Error", message: "This is a error", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
}

sequence

Upvotes: 1

Views: 802

Answers (3)

Tyler Rutt
Tyler Rutt

Reputation: 597

This is because the second controller has basically just been placed on the top of the existing one. The first view is still running under the second view, and when the second view is dismissed the first view won't call ViewDidLoad. So to solve it, you probably want to add it inside the ViewDidAppear Function.

Upvotes: 1

Garfield81
Garfield81

Reputation: 521

Instead of having the DescriptionController dismiss itself, a better way would be for it would be to instead inform the ViewController that the user has signed in and also returns any errors, if necessary. Then the ViewController can perform any additional steps needed based on successful or failed sign-in. This could be accomplished by using the delegate pattern (DescriptionController defines a protocol and ViewController implements it).

Upvotes: 0

Jupiter Cls
Jupiter Cls

Reputation: 291

Use this code in ViewdidAppear:

FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
        if user != nil {
           self.dismiss(animated: false, completion: nil)
        }

    }

    sign_in_custom.addTarget(self, action: #selector(changeToSingIn), for: [.touchUpInside])

Upvotes: 1

Related Questions