Ios 在视图之间快速传递数据
我有一个注册页面,要求用户输入:用户名、密码、电子邮件、全名。单击“注册”按钮时,信息将发送到我的数据库 我想做的是在一个视图控制器中显示用户名文本字段,输入该信息后,用户名准备好输入他们的全名,他们可以点击同一视图控制器上的下一步按钮,它将转到另一个具有全名文本字段的视图控制器,依此类推。我想知道如何在视图控制器中移动而不丢失以前的视图控制器值。在最终视图控制器上,用户将单击“注册” 我的注册按钮代码为:Ios 在视图之间快速传递数据,ios,swift,uitextfield,Ios,Swift,Uitextfield,我有一个注册页面,要求用户输入:用户名、密码、电子邮件、全名。单击“注册”按钮时,信息将发送到我的数据库 我想做的是在一个视图控制器中显示用户名文本字段,输入该信息后,用户名准备好输入他们的全名,他们可以点击同一视图控制器上的下一步按钮,它将转到另一个具有全名文本字段的视图控制器,依此类推。我想知道如何在视图控制器中移动而不丢失以前的视图控制器值。在最终视图控制器上,用户将单击“注册” 我的注册按钮代码为: // create new user and send results to mysql
// 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()
见最后更新的答案(我必须在发问者的评论后添加): 如果您对用户名、密码等所有字段使用UITextField 建议的方法是检查名为
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
}
要解决下一个问题,可以将键盘的返回键更改为next
关于您的问题的一些建议:
如何设置它,使每个文本字段都有自己的视图?例如,当用户键入用户名时,他们可以单击“下一步”,它将转到下一个显示全名文本字段的视图,而不会丢失用户名文本字段的值
首先,save/retain在
UserDefaults
中输入的上一个输入值。因此,当用户导航回上一个屏幕时,您可以从UserDefaults
中检索该值。对不起,我想我解释错了。我对斯威夫特很陌生,所以请原谅我缺乏知识。我想做的是在一个视图控制器中显示用户名文本字段,输入该信息后,用户名准备好输入其全名,他们可以点击同一视图控制器上的下一步按钮,它将转到另一个具有全名文本字段的视图控制器,依此类推。我想知道如何在视图控制器中移动而不丢失以前的视图控制器值。在最终视图控制器上,用户将单击“注册”