提问者:小点点

如何将数据从一个ViewController传递到另一个Xcode?


所以我有一个小测验,当小测验结束时,我想显示有多少答案是正确的。 下面是我试着去做的。

这是第一个VC

import UIKit

class QuizViewController: UIViewController {



@IBOutlet weak var timer: UILabel!
@IBOutlet weak var question: UILabel!
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var prasanje: UILabel!

//odgovori

@IBOutlet weak var odgovor1: UIButton!
@IBOutlet weak var odgovor2: UIButton!
@IBOutlet weak var odgovor3: UIButton!

let allQuestions = QuestionBank()
let randomQuestions = QuestionBank.shared.getRandomQuestions(6)
var questionNumber: Int = 0
var score: Int = 0
var selectedAnswer: Int = 0
let updateT = EndViewController()

var taimer:Timer?
var seconds: Int = 60

override func viewDidLoad() {
    super.viewDidLoad()

    updateQuestion()
    updateUI()

}

@IBAction func Back(_ sender: Any) {
     let dialogMessage = UIAlertController(title: "Потврдете", message: "Дали сте сигурни дека сакате да прекинете?", preferredStyle: .alert)

    let ok = UIAlertAction(title: "Да", style: .default, handler: { (action) -> Void in
                print("Ok button tapped")
        self.dimiss()
           })

    let cancel = UIAlertAction(title: "Откажи", style: .cancel) { (action) -> Void in
               print("Cancel button tapped")
           }

    dialogMessage.addAction(ok)
    dialogMessage.addAction(cancel)

    self.present(dialogMessage, animated: true, completion: nil)
}

func dimiss(){
    self.dismiss(animated: true, completion: nil)
}

@IBAction func odgovorpotvrden(_ sender: UIButton) {
    if sender.tag == selectedAnswer {
        print("correct")
        score += 1
    }else {
        print("wrong")
    }
    questionNumber += 1
    updateQuestion()
}

func updateQuestion() {

    if questionNumber <= randomQuestions.count - 1{
    image.image = UIImage(named:(randomQuestions[questionNumber].image))
    prasanje.text = randomQuestions[questionNumber].prasanje
    odgovor1.setTitle(randomQuestions[questionNumber].odgovor1, for: UIControl.State.normal)
    odgovor2.setTitle(randomQuestions[questionNumber].odgovor2, for: UIControl.State.normal)
    odgovor3.setTitle(randomQuestions[questionNumber].odgovor3, for: UIControl.State.normal)
    selectedAnswer = randomQuestions[questionNumber].tocenodgovor
    }
    else {
        endVC()
    }
    updateUI()


}

func updateUI() {
    question.text = "\(questionNumber)/\(randomQuestions.count)"

    let min = (seconds / 60) % 60
    let sec = seconds % 60

    timer!.text = String(format: "%02d", min) + ":" + String(format: "%02d", sec)

    time()
}

func time() {

    if taimer == nil {
        taimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
            if self.seconds == 0{
                self.timeup()
            }else if self.seconds <= 60 {
                self.seconds -= 1
                self.updateUI()
            }
        }
    }

}

func timeup() {
    taimer?.invalidate()
    taimer = nil

   let timeup = UIAlertController(title: "Времето истече", message: "Крај на испитот. Дали сакате да започнете од почеток?", preferredStyle: .alert)
    let restart = UIAlertAction(title: "Ресетирај", style: .default, handler: { (action) -> Void in
                print("restart button tapped")
                    self.restartQuiz()})

    let dismiss = UIAlertAction(title: "Откажи", style: .default, handler: { (action) -> Void in
                    print("dismiss button tapped")
                        self.dimiss()})
    timeup.addAction(restart)
    timeup.addAction(dismiss)

    self.present(timeup, animated: true, completion: nil)
}
func updateScore(){
    if score > randomQuestions.count - 2 {
        updateT.no1 = "Cestitki"

    }else {
        updateT.no1 = "uaa"

    }

}

func restartQuiz() {
    seconds = 60
    score = 0
    questionNumber = 0
    updateQuestion()

}

@IBAction func endVC() {
      let sb = UIStoryboard(name: "Main", bundle: nil)

      if let endVC    = sb.instantiateViewController(identifier: "endVC") as? EndViewController{
          self.present(endVC, animated: true, completion: nil)

      }
}

这是我试图将数据传递给的VC。

import UIKit

class EndViewController: UIViewController {




@IBOutlet weak var cestitki: UILabel!
@IBOutlet weak var skor: UILabel!

var no1: String = ""
var no2: String = ""

override func viewDidLoad() {
    super.viewDidLoad()

    cestitki!.text = no1


}

但是当我运行程序时,标签是空白的,什么都没有。

我要么试图传递“分数”值从一个到另一个VC或改变标签从第一个VC在第二。 两者都有帮助


共1个答案

匿名用户

这个问题不是很清楚,所以这是将数据FirstVc传递到SecondVC的一些解决方案

如果使用segue将第一个ViewController更改为第二个ViewController,则可以传递如下数据:

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if segue.destination is secondVC
    {
        let vc = segue.destination as? secondVC
        vc?.no1 = "your data"
    }
}

如果您正在使用NavigationController,则可以使用以下方法传递数据:

    let secondVc = self.storyboard!.instantiateViewController(withIdentifier: "seconvc ID") as! secondViewController

    secondVC.no1 = "your data"
    self.navigationController?.pushViewController(photoSelectionVc, animated: true)