首页 > 解决方案 > 在快速加载页面之前等待json数据下载

问题描述

将数据库中的 json 代码解析到 iphone uilabel 时出现问题。该页面似乎加载速度更快,可以下载 json 数据。每次我运行它时,它总是留下一个空的 UIlabel,但它确实显示在打印上。我如何做到这一点,以便页面将等待 json 被下载,然后它可以继续显示其中包含 json 内容的页面。

该页面,我正在使用放置在 viewcontroller 内的 uiview

class ContentView: UIView {


    let request = requestQuestion()
    var currentVC:ViewController?
    var times = "10"
    let time = UILabel()
    let textViews = UILabel()
    let recommendList = recommend()
    let choices = ChoicesCollections()
    var content = ""

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupView(){
        content = request.getquestion(questions: "212", url: "http://localhost/memory/question.php")
        print(content)
        time.translatesAutoresizingMaskIntoConstraints = false
        textViews.translatesAutoresizingMaskIntoConstraints = false
        recommendList.translatesAutoresizingMaskIntoConstraints = false
        choices.translatesAutoresizingMaskIntoConstraints = false

        addSubview(time)
        addSubview(textViews)
        addSubview(recommendList)
        addSubview(choices)

        time.text = times
        time.font = UIFont.boldSystemFont(ofSize: 20)
        time.textAlignment = .center
        time.topAnchor.constraint(equalTo: topAnchor, constant: 40).isActive = true
        time.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
        time.widthAnchor.constraint(equalToConstant: 50).isActive = true
        time.heightAnchor.constraint(equalToConstant: 25).isActive = true

        textViews.numberOfLines = 7
        textViews.font = UIFont.boldSystemFont(ofSize: 30)
        textViews.text = content
        textViews.textAlignment = .center
        textViews.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
        textViews.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        textViews.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        textViews.bottomAnchor.constraint(equalTo: centerYAnchor, constant: 100).isActive = true

        choices.topAnchor.constraint(equalTo: textViews.bottomAnchor, constant: 60).isActive = true
        choices.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        choices.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        choices.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

        recommendList.backgroundColor = .black
        recommendList.bottomAnchor.constraint(equalTo: choices.topAnchor,constant: -5).isActive = true
        recommendList.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        recommendList.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        recommendList.heightAnchor.constraint(equalToConstant: 50).isActive = true
    }
}

json页面

struct question:Decodable {
        let question:String
        let boxA:String
        let boxB:String
        let boxC:String
        let boxD:String
        let categories:String
    }
func getquestion(questions:String,url:String)->String{
        //return "which state that are bordering mexico?"
        let request = NSMutableURLRequest(url: NSURL(string: url)! as URL)
        request.httpMethod = "POST"
        let postString = "question=\(questions)"
        request.httpBody = postString.data(using: .utf8)
        var returns = ""
        let task = URLSession.shared.dataTask(with: request as URLRequest) {
            data, response, error in
            if error != nil {
                print("error=\(String(describing: error))")
                return
            }
            // read the response from php
            do {
                let decoder = JSONDecoder()
                let product = try decoder.decode(question.self, from: data!)
                DispatchQueue.main.async{
                    returns = product.question
                }

            } catch let error as NSError {
                print(error)
            }
        }
        task.resume()
        return returns
    }

标签: iosjsonswift

解决方案


你需要一个完成

func getquestion(questions:String,url:String,completion:@escaping((_ str:String?)->())){
  //return "which state that are bordering mexico?"
  var request = URLRequest(url: URL(string: url)!)
  request.httpMethod = "POST"
  let postString = "question=\(questions)"
  request.httpBody = postString.data(using: .utf8)
  var returns = ""
  let task = URLSession.shared.dataTask(with: request as URLRequest) {
    data, response, error in
    if error != nil {
      print("error=\(String(describing: error))")
      completion(nil)
      return
    }
    // read the response from php
    do {
      let decoder = JSONDecoder()
     // let product = try decoder.decode(question.self, from: data!)
      DispatchQueue.main.async{
        completion(product.question)
      }

    } catch  {
      print(error)
      completion(nil)
    }
  }
  task.resume()

}

content = request.getquestion(questions: "212", url: "http://localhost/memory/question.php")
   { (str) in
  print(str)
}

推荐阅读