首页 > 解决方案 > 如何将 JSON 数据从 viewController 传递到 UIView 以用于标签

问题描述

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

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

 }

标签: iosswift

解决方案


你的问题在这里

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 以获取动画值:true


推荐阅读