我目前正在尝试找到一种方法将从我的 API 请求收到的 JSON 传递到我的 UIView,以便它可以用于标签文本,但是我不使用 Storyboard,并且我所有的 View Controller 都是以编程方式制作的,所以下面是我正在尝试获取数据的文件以及我正在接收 View Controller 中的数据的函数
profileView.swift
import Foundation
import UIKit
import PureLayout
class profileView: UIView {
var shouldSetupConstraints = true
var profileImageContainer = UIView()
var profileImage = UIImageView(image: UIImage(named: "logo"))
var first_name = UILabel()
var last_name = UILabel()
let screenSize = UIScreen.main.bounds
let gradient = CAGradientLayer()
let textFieldAppearance = UITextField.appearance()
override init(frame: CGRect) {
super.init(frame: frame)
gradient.colors = [UIColor(red: (0/255.0), green: (114/255.0), blue: (255/255.0), alpha: 1).cgColor , UIColor(red: (0/255.0), green: (198/255.0), blue: (255/255.0), alpha: 1).cgColor]
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 1, y: 0)
gradient.cornerRadius = 4
profileImage.center = CGPoint(x: screenSize.width/2, y:screenSize.height/2)
self.addSubview(profileImage)
first_name.frame = CGRect(x: screenSize.height/11, y: screenSize.width/2, width: 100, height: 50)
first_name.font = UIFont(name: "HelveticaNeue-light", size: 24)
first_name.textColor = .black
self.addSubview(first_name)
textFieldAppearance.keyboardAppearance = .light
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func updateConstraints() {
if(shouldSetupConstraints) {
// AutoLayout constraints
shouldSetupConstraints = false
}
super.updateConstraints()
}
}
mainViewController.swift
@objc func login(_ sender: UIButton){
let url = URL(string: "http://myapi.com/login.php")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
//getting values from text fields
let email = loginViewer.email.text
let password = loginViewer.password.text
//creating the post parameter by concatenating the keys and values from text field
let postParameters = "email="+email!+"&password="+password!;
//adding the parameters to request body
request.httpBody = postParameters.data(using: String.Encoding.utf8)
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
if error != nil{
print(error!)
return
}
//parsing the response
do {
//converting resonse to NSDictionary
let myJSON = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
//parsing the json
if let parseJSON = myJSON {
//creating a string
var msg : String!
var first_name : String!
var last_name : String!
//getting the json response
msg = parseJSON["message"] as! String?
first_name = parseJSON["first_name"] as! String?
last_name = parseJSON["last_name"] as! String?
//printing the response
print(msg)
print(first_name)
print(last_name)
let profileView = profile()
profileView.first_name = first_name
self.present(profile(), animated: true, completion: nil)
}
} catch {
print(error)
}
}
//executing the task
task.resume()
}
最佳答案
你的问题就在这里
let profileView = profile()
profileView.first_name = first_name
self.present(profile(), animated: true, completion: nil)
首先你不能呈现 View ,只能呈现 VC,因此你需要将此 View 嵌入到 VC 中,OR
var profileV:profileView!
//
DispatchQueue.main.async {
self.profileV = profileView(frame:self.view.frame)
self.profileV.first_name.text = first_name
self.view.addSubview(profileV)
}
删除
self.profileV.removeFromSuperview()
downSide 缺少动画,因此将其插入到 VC 中以获取 animated:true 的值
关于ios - 如何将 JSON 数据从 viewController 传递到 UIView 以用于标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52029817/