简体   繁体   中英

How to navigate from UIView to a UIView Controller using segues?

Aim: To show the View Controller - 'Welcome Page' with the button "Continue Here" from the previous view controller - 'Login Page'.

I have been making this IOS app on swift 3 in Xcode 8.2 that has a login page coded programmatically and would like to transit to another page called the Welcome page that has a button on the screen in Storyboard.

故事板

I'm having problems in connecting a view controller that has been coded programmatically to another view controller containing buttons that have been dragged on the storyboard. I have read many articles and watched many tutorials on constructing a segue programmatically and using them. Have also researched in using protocols to solve my problem but for some reason, I'm having an error.

I have tried implementing segues and giving them an identifier and then presenting them but didn't work as well. About what I meant by trying is I have used different commands such as dismiss, present, show but it did not view the button present on the next view controller. Here is the function of view controller class - 'Login Page' that has to move to the next view controller after the if statement so that it can show the view controller - 'Login Page' with the button.

func handleLogin() {

    //Guard statements are useful for form validation
    guard let email = emailTextField.text, let password = passwordTextField.text
        else{
            print("Form not valid")
            return
    }

    Auth.auth().signIn(withEmail: email, password: password) { (user, error) in

        if error != nil{

            let alert = UIAlertController(title: "Sorry!", message: "Incorrect Credentials", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)

            print(error)
            return
        }
        //Need to do the code here that can display the next view controller with the button
    }
}

Kindly help, please.

To segue programmatically to a Storyboard view, you'll need to instantiate the next view controller then call pushViewController if using a navigationController or you can use self.present() . Here is an example using a navigationController.

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
    return controller
self.navigationController?.pushViewController(controller, animated: true)

Note that you should change the storyboard name "Main" to whatever your storyboard filename is. You should also change "NextViewController" to whatever your controller storyboard identifier is (see Storyboard ID in image below). Also make sure to set your custom class in the storyboard (see Custom Class in image below).

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM