I have a registration page that requires the user to enter: username, password, email, full name. When the register button is clicked the information is sent to my database
What I want to do was to display the username textfield in one view controller, and after that information has been entered, and the username is ready to enter their full name, they can hit a next button on the same view controller and it will go to a different view controller with the full name textfield and so on. I was wondering how I could move through the view controllers without losing the previous view controllers value. At the final view controller, the user will click register
My code for the register button is:
// create new user and send results to mysql database
let url = NSURL (string: "http:/websitename.php")!
let request = NSMutableURLRequest (url: url as URL)
request.httpMethod = "POST"
print ("Request: \(request)")
// body for the request
let body = "user_username=\(usernameTxt.text!.lowercased())&user_password=\(passwordTxt.text!)&user_email=\(emailTxt.text!)&user_fullname=\(fullnameTxt.text!)"
request.httpBody = body.data(using: String.Encoding.utf8)
print("Request: \(request)")
print("RequestBody: \(String(describing: request.httpBody))")
URLSession.shared.dataTask(with: request as URLRequest, completionHandler: {(data:Data?, response: URLResponse?, error: Error?) in
// no error
if (error == nil) {
// send the request to the server
print("Going to send the request to the server")
// communicate back to UI
DispatchQueue.main.async {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
guard let parseJSON = json else {
print ("There was an error while parsing the JSON data")
return
}
// get the ID from the JSON struct
let id = parseJSON["id"]
// if there is an ID then login
if (id != nil) {
// save user information we received from our host
UserDefaults.standard.set(parseJSON, forKey: "parseJSON")
user = UserDefaults.standard.value(forKey: "parseJSON") as? NSDictionary
// go to tabbar or homepage
DispatchQueue.main.async {
appDelegate.login()
}
} else {
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = parseJSON["message"] as! String
appDelegate.infoView(message: message, color: colorSmoothRed)
})
}
} catch {
// communicate back to UI
DispatchQueue.main.async(execute: {
let message = error as! String
appDelegate.infoView(message: message, color: colorSmoothRed)
})
}
}
} else {
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = error!.localizedDescription
appDelegate.infoView(message: message, color: colorSmoothRed)
})
}
// launch prepared session
}).resume()
See Updated Answered At The End (Which I had to add after the comment of questioner):
If you are using UITextField for all the Field like username, password etc.
The recommended way is to check the UITextFieldDelegate method called,
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == usernameField {
fullNameField.becomeFirstResponder()
} else if textField == fullNameField {
emailField.becomeFirstResponder()
} else if textField == emailField {
passwordField.becomeFirstResponder()
} else {
textField.resignFirstResponder()
}
usernameField.becomeFirstResponder()
return true
}
to solve the next problem, you can change the return key of the keyboard to next
Some Suggestion regarding your question:
How can I set it up so that each textfield has its own view? For example when the user types in the username, they can click next, and it'll go to the next view that displays the full name textfield without losing what the value of the username textfield was?
Making a view for each textField will create a bad user experience, plus why you want to increase your redundant code.
Try to create as minimal Boilerplate code .
Here is some website to which top developers refers to make great designs:
ANSWER AS PER THE COMMENT OF QUESTIONER:
First, save/retain the previous enter value entered in UserDefaults
. So that when the user navigates back to the Previous screen, you can retrieve that value from UserDefaults
.
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.