如何将 JSON 数据从 viewController 传递到 UIView 以用于标签



我目前正在尝试找到一种方法将从我的API请求收到的JSON传递给我的UIView,以便它可以用于标签文本,但是我不使用情节提要,我所有的视图控制器都是以编程方式制作的,所以下面是我尝试将数据获取到的文件以及我接收数据的功能在我的视图控制器中

个人资料视图.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()
}

}

主视图控制器.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)

首先你不能呈现一个视图,只能呈现一个VC,所以你需要把这个视图嵌入到一个VC中,或者

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()

缺点是缺少动画,所以将其插入VC中以获取animated:true的值

最新更新