从 Web 服务器传递数据并将其显示在视图控制器上
我有来自服务器的数据,需要在另一个视图控制器上显示。 注意我执行了邮政请求。 note 我有2个viewControllers
这些字段:
firstName
lastName
emailAddress
mobilenumber
这是启动Web服务帖子(数据下载)的代码,
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let profileVC = segue.destination as? ProfileViewController else {return}
profileVC.nameLabel.text = self.firstName
profileVC.idNumberLabel.text = self.idNumber
profileVC.emailAddressLabel.text = self.emailAddress
profileVC.mobileNumberLabel.text = self.mobileNumber
这是下载的数据应该在这里显示
private func updateUI(firstName: String, lastName: String, emailAddress: String, mobileNumber: String) {
self.nameLabel.text = firstName + "" + lastName
self.idNumberLabel.text = idNumber
self.emailAddressLabel.text = emailAddress
}
这是发布请求代码
// Prepare URL
let url = URL(string: stringURL)
guard let requestUrl = url else { fatalError() }
// Prepare URL Request Object
var request = URLRequest(url: requestUrl)
request.httpMethod = "POST"
// Set HTTP Request header
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("1088", forHTTPHeaderField: "Content-Length")
// HTTP Parameters which will be sent in HTTP Request body
let postString = "userID=\(useridTextField.text)&password=\(passwordTextField.text)";
// Set HTTP Request Body
request.httpBody = postString.data(using: String.Encoding.utf8)
// Perform HTTP Request
let session = URLSession.shared
let task = session.dataTask(with: request) { data, response, error in
guard let data = data else {
return
}
do {
var response = try JSONDecoder().decode(User.self, from: data)
response.idNumber = self.idNumber
response.firstName = self.firstName
response.middleName = self.middleName
response.lastName = self.lastName
response.emailAddress = self.emailAddress
response.mobileNumber = self.mobileNumber
response.landline = self.landline
return
} catch {
let str = String(decoding: data, as: UTF8.self)
print(str)
}
}
task.resume()
I have data from a server which I need to show on another View Controller.
Note I had perform a POST request.
Note I have 2 ViewControllers
these are the fields:
firstName
lastName
emailAddress
mobileNumber
This is the code that started the web service POST (data downloaded)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let profileVC = segue.destination as? ProfileViewController else {return}
profileVC.nameLabel.text = self.firstName
profileVC.idNumberLabel.text = self.idNumber
profileVC.emailAddressLabel.text = self.emailAddress
profileVC.mobileNumberLabel.text = self.mobileNumber
This is where the downloaded data is supposed to show
private func updateUI(firstName: String, lastName: String, emailAddress: String, mobileNumber: String) {
self.nameLabel.text = firstName + "" + lastName
self.idNumberLabel.text = idNumber
self.emailAddressLabel.text = emailAddress
}
this is the post request code
// Prepare URL
let url = URL(string: stringURL)
guard let requestUrl = url else { fatalError() }
// Prepare URL Request Object
var request = URLRequest(url: requestUrl)
request.httpMethod = "POST"
// Set HTTP Request header
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("1088", forHTTPHeaderField: "Content-Length")
// HTTP Parameters which will be sent in HTTP Request body
let postString = "userID=\(useridTextField.text)&password=\(passwordTextField.text)";
// Set HTTP Request Body
request.httpBody = postString.data(using: String.Encoding.utf8)
// Perform HTTP Request
let session = URLSession.shared
let task = session.dataTask(with: request) { data, response, error in
guard let data = data else {
return
}
do {
var response = try JSONDecoder().decode(User.self, from: data)
response.idNumber = self.idNumber
response.firstName = self.firstName
response.middleName = self.middleName
response.lastName = self.lastName
response.emailAddress = self.emailAddress
response.mobileNumber = self.mobileNumber
response.landline = self.landline
return
} catch {
let str = String(decoding: data, as: UTF8.self)
print(str)
}
}
task.resume()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是一个使用 Publish/Observer 模式和
Notifications
的示例我从这个简单的扩展开始,它可以帮助我组织通知名称
假设您有一个带有启动请求函数的 Network 类,可以启动您的网络请求。
一旦你成功获得响应并解码了对 URL 对象的响应,你就可以向任何观察者发布/发布通知
然后在需要更新一些数据的模块中,你需要观察/监听这个通知
例如,假设这是一个视图控制器,您需要观察此通知,当您收到此事件的通知时,您需要对其进行管理。
尝试一下,看看是否对您有帮助。
如果您正在寻找更复杂的东西,例如绑定,您可以看看这个 结合教程。
Here is an example using Publish/Observer pattern with
Notifications
I start with this simple extension that helps me organize notification names
Let's say you have a Network class with a launch request function that launches your network request.
Once you have successfully got a response and decoded your response to a URL object, you can publish / post a notification to any observers
Then in the module where you need some data to be updated, you need to observe / listen this notification
For example, let's say this was a view controller, you need to observe this notification and when you get notified of this event, you need to manage this.
Give this a go and see if it helps you.
If you are looking for something more sophisticated like binding, you can have a look at this Combine tutorial.