0
votes

I have issue with my app. Scenario is simple, after successfully account creation i wan't to dismiss current page or navigate to login page. My storyboard looks like this:

enter image description here

After successful account creation i having a popup with some info about it's ok, we send you verification email and after this popup i want to go to the page second from left - it's my main application page (now called "View Controller").

I tried dismiss window, but i have no effect there, it can only dismiss my popup window. When i trying to redirect then i have issue with back button when is pressed,it lead to Sign Up page. There is some code:

// Create new user and send verification email

        Auth.auth().createUser(withEmail: userEmail, password: userPassword) { user, error in if error == nil && user != nil {
            self.sendVerificationMail();
            self.displayAlertMessage(alertTitle: "Success", alertMessage: "Your account created successfully. We send you a verification email.");

            // Redirect to View Controller

            } else {
            self.displayAlertMessage(alertTitle: "Unhandled error", alertMessage: "Undefined error #SignUpViewController_0002");
            }
        }

...

func displayAlertMessage(alertTitle: String, alertMessage:String, alertRedirection:String = ""){
        let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert);

        let okAction = UIAlertAction(title:"Ok", style: UIAlertAction.Style.default, handler: nil);

        alert.addAction(okAction);

        self.present(alert, animated:true, completion:nil);
    }

If i add this:

self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)

After alert, it close only alert, before alert, it do nothing ( same as dismiss).

3
you need to add action to your ok buttonAndres Gomez
@AndresGomes if i add it, i have issue with back button and i shouldn't be able to get into sign up page if i logged, but if i press back im redirected to sign up pageYardi
nop, when you press your ok button, the app will navigate to loginviewcontroller, its easy, check how your push a view controller with navigation controller, when the app is in loginviewcontroller, you need to configure the back button, if not, the app come back to registerviewcontrollerAndres Gomez
@AndreasGomes - self.performSegue(withIdentifier: "test", sender: self); - i add this to ok button and after press i am on valid page, but i have back button and if i press it im again on sign up page - how to avoid this?Yardi
@iSheep - Don't use performSegue in 'ok' button action. It will add more views to the navigation controller. Pop to your necessary view controller(main view) from the current view and dismiss the alert within the alert action.RAJA

3 Answers

1
votes

To dismiss and pop to main view you can use alert button action handler.

alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: { (action) in

            self.dismiss(animated: true, completion: {
                self.navigationController?.popToRootViewController(animated: true)
            })
        }))

Or you can use the navigation to specific view controller using below lines.

for viewController in self.navigationController!.viewControllers {
            if viewController.isKind(of: <Your_Main_ViewController_Class_Name>.self) {
                self.navigationController?.popToViewController(viewController, animated: true)
                break
            }
        }

Your_Main_ViewController_Class_Name is the view controller that within your navigation controller stack to which you need to navigate. (ie) main view

To blindly navigate to main view once alert popup displayed, you can use completion handler while present the alert.

self.present(alert, animated: true, completion: {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
                self.navigationController?.popToRootViewController(animated: true)
            }
        })
0
votes

well, you are using a navigation controller, for "present" a new view controller, you need to push it, for example.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("IDOFYOURVIEW") as CLASS_NAME_OFYOUR_VIEWCONTROLLER
navigationController?.pushViewController(vc, animated: true)

with the last code you can "present" (push) a new view controller

Now, if you want to make a other action when your press backbutton, try with this lines

override func viewDidLoad {
    super.viewDidLoad()
    self.navigationItem.hidesBackButton = true
    let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Bordered, target: self, action: "back:")
    self.navigationItem.leftBarButtonItem = newBackButton
}

func back(sender: UIBarButtonItem) {
    //in this part you can move to other view controller, examples
    // Go back specific view in your navigation controller
    for controller in self.navigationController!.viewControllers as Array {
    if controller.isKind(of: NAMECLASS_OFYOUR_VIEWCONTROLLER.self) {
        _ =  self.navigationController!.popToViewController(controller, animated: true)
        break
       }
    }
    // Perform your custom actions
    // ...
    // Go back to the previous ViewController
    self.navigationController?.popViewControllerAnimated(true)
}

Regards

0
votes

View Controller class:

import UIKit

class ViewController: UIViewController {

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

    override func viewDidAppear(_ animated: Bool) {
        self.performSegue(withIdentifier: "notLoggedView", sender: self);
    }
}

Class id set to "test":

enter image description here

"Pushing" :

...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "test") as ViewController
            navigationController?.pushViewController(vc, animated: true)
...

And sadly error:

'UIViewController' is not convertible to 'ViewController'

Where i made mistake?