gpt4 book ai didi

ios - 在 View 之间快速传递数据

转载 作者:搜寻专家 更新时间:2023-11-01 07:10:54 27 4
gpt4 key购买 nike

我有一个注册页面,要求用户输入:用户名、密码、电子邮件、全名。单击注册按钮后,信息将发送到我的数据库 enter image description here

我想做的是在一个 View Controller 中显示用户名文本字段,在输入该信息后,用户名已准备好输入他们的全名,他们可以在同一个 View Controller 上点击下一步按钮,然后它将转到具有全名文本字段等的不同 View Controller 。我想知道如何在不丢失以前的 View Controller 值的情况下移动 View Controller 。在最终的 View Controller 中,用户将单击注册

我的注册按钮代码是:

// 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

推荐的方法是检查调用的 UITextFieldDelegate 方法,

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

enter image description here

关于您的问题的一些建议:

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?

  1. 为每个 textField 创建一个 View 会造成糟糕的用户体验,以及为什么要增加冗余代码。

  2. 尝试创建最小的 Boilerplate code .

以下是顶级开发人员在进行出色设计时提到的一些网站:

  1. pttrns : Login
  2. lovely ui : Login
  3. Inspired UI : Login

根据提问者的评论回答:

首先,保存/保留之前在UserDefaults 中输入的输入值。这样当用户导航回到上一个屏幕时,您可以从 UserDefaults 中检索该值。

关于ios - 在 View 之间快速传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45003572/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com