I am currently working on a social media app and so far I have been able to make a LoginViewController & a SignUpVIewController, and have it where when the user taps on the Sihn Up Button on the LoginViewController it takes them to the SignUpViewCOntroller. Right now I am currently trying to have it where on the SignUpViewController if not all required fields are filled out then the app will display an alert. However, every time I run the app the alert doesn't show up on the simulator screen.
My code for the SignUpViewController is as follows:
import UIKit
import Parse
class SignUpViewController: UIViewController
{
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var confirmPasswordFieldText: UITextField!
@IBOutlet weak var signUpButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func signUpButtonTapped(_ sender: UIButton)
{
let username = usernameTextField.text
let password = passwordTextField.text
let email = emailTextField.text
let confirmpw = confirmPasswordFieldText.text
if username == "" || password == "" || email == "" || confirmpw == ""
{
let alert = UIAlertController(title: "Error", message: "Please fill out all required fields", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default,handler: nil))
present(alert, animated: true, completion: nil)
return
}
let newUser = User(username: username!, password: password!, email: email!)
}
}
class User
{
var username: String
var password: String
var email: String
init(username: String, password: String, email: String) {
self.username = username
self.password = password
self.email = email
}
}
Essentially when I tap the SignUp button on my SignUpViewController while leaving all the fields blank it still takes me to the next view controller (which I plan to have been the profile creation view controller), instead of showing the alerts. All the buttons are connected to the correct spots. Would greatly appreciate it if anyone can help me fix this.
It appears (I'm making an assumption here but without a screenshot of your storyboard it is hard to be certain) you may have connected the button in your storyboard to the next view controller via a segue. If that's the case then it would automatically move to the next view controller. So, here you should remove your segue inside the storyboard and programatically navigate to the next screen using your @IBAction
method.
Also, unrelated to this, but @INFINITE's comment is also a good observation. You should probably use a guard
statement here like below:
@IBAction func signUpButtonTapped(_ sender: UIButton) {
guard let username = usernameTextField.text,
let password = passwordTextField.text,
let email = emailTextField.text,
let confirmpw = confirmPasswordFieldText.text else {
// It may be a good idea to refactor this bit into a separate method
let alert = UIAlertController(title: "Error", message: "Please fill out all required fields", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
return
}
let newUser = User(username: username, password: password, email: email)
// present the next view controller here
let storyboardInstance = UIStoryboard.init(name: "YOUR_STORYBOARD_NAME", bundle: Bundle.main)
if let profileViewController = storyboardInstance.instantiateViewController(withIdentifier: "YOUR_VIEWCONTROLLER_STORYBOARD_ID") {
// set whatever your have to on the profile view controller here
// profileViewController.user = newUser
// then,
// either push the next view controller
// navigationViewController?.pushViewController(profileViewController, animated: true)
// or present it
// present(profileViewController, animated: true)
// or do whatever else is needed
}
}