在没有 segue 的多个视图控制器之间共享数据(Swift 3) [英] Sharing data between multiple view controllers without segue (Swift 3)

查看:22
本文介绍了在没有 segue 的多个视图控制器之间共享数据(Swift 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 QR 码扫描仪测验应用程序,其中用户必须从 10 个问题中获得 10 分.在用户第 1 个 qn 之后,分数将加 1,它会将他们恢复到 qr 扫描仪页面,在那里他们必须扫描下一个 qn 的 QR 码.问题是分数数据的传递.有没有办法在没有 segue 的情况下做到这一点?

I'm doing a QR code scanner quiz app where users must get a score of 10 from doing 10 questions. After a user the 1st qn, the score will plus 1 and it will revert them back to the qr scanner page where they must scan the QR code for the next qn. The problem is the passing of the score data. Is there a way to do it without segue?

这是我的 qn1controller

This is my qn1controller

import UIKit

class Quiz1Controller: UIViewController {

    @IBOutlet var question: UILabel!
    @IBOutlet var button1: UIButton!
    @IBOutlet var button2: UIButton!
    @IBOutlet var button3: UIButton!
    @IBOutlet var button4: UIButton!
    @IBOutlet var LabelEnd: UILabel!
    @IBOutlet var scorelabel: UILabel!
    var score = Int()
    var CorrectAnswer = String()

    override func viewDidLoad() {
        super.viewDidLoad()

        RandomQuestions()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func RandomQuestions(){

        var RandomNumber = arc4random() % 1
        RandomNumber += 1

        switch(RandomNumber){
        case 1:
            question.text = "Who is the current Deputy Chairman of People's Association?"
            button1.setTitle("Lee Hsien Loong", for: .normal)
            button2.setTitle("Chan Chun Sing", for: .normal)
            button3.setTitle("Goh Chok Tong", for: .normal)
            button4.setTitle("Goh Khen Swee", for: .normal)
            CorrectAnswer = "2"
            Hide()
            break
        default:
            break
        }
    }

    func Hide(){
        LabelEnd.isHidden = true
    }
    func UnHide(){
        LabelEnd.isHidden = false
    }

    @IBAction func Button1(_ sender: Any) {
        UnHide()

        if (CorrectAnswer == "1"){
            self.performSegue(withIdentifier: "correct", sender: self)
        }
        else {
            LabelEnd.text = "Incorrect Answer! Try again"
        }
    }

    @IBAction func Button2(_ sender: Any) {
        UnHide()

        if (CorrectAnswer == "2"){
            score = score + 1
            scorelabel.text = "Score:\(score)"
            self.performSegue(withIdentifier: "correct", sender: self)            
        }
        else {
            LabelEnd.text = "Incorrect Answer! Try again"
        }
    }

    @IBAction func Button3(_ sender: Any) {
        UnHide()

        if (CorrectAnswer == "3"){
            self.performSegue(withIdentifier: "correct", sender: self)
        }
        else {
            LabelEnd.text = "Incorrect Answer! Try again"
        }
    }

    @IBAction func Button4(_ sender: Any) {
        UnHide()

        if (CorrectAnswer == "4"){
            self.performSegue(withIdentifier: "correct", sender: self)
        }
        else {
            LabelEnd.text = "Incorrect Answer! Try again"
        }
    }

}

这是我的 qn2 控制器

And this is my qn2 controller

import UIKit

class Quiz2Controller: UIViewController {

    @IBOutlet var question: UILabel!
    @IBOutlet var button1: UIButton!
    @IBOutlet var button2: UIButton!
    @IBOutlet var button3: UIButton!
    @IBOutlet var button4: UIButton!
    @IBOutlet var LabelEnd: UILabel!
    @IBOutlet var scorelabel: UILabel!
    var score = Int()
    var CorrectAnswer = String()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        RandomQuestions()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

    func RandomQuestions(){

        var RandomNumber = arc4random() % 1
        RandomNumber += 1

        switch(RandomNumber){
        case 1:
            question.text = "Who is the founder of People's Association?"
            button1.setTitle("Lee Hsien Loong", for: .normal)
            button2.setTitle("Lee Kuan Yew", for: .normal)
            button3.setTitle("Goh Chok Tong", for: .normal)
            button4.setTitle("Goh Khen Swee", for: .normal)
            CorrectAnswer = "1"
            Hide()
            break
        default:
            break
        }
    }

    func Hide(){
        LabelEnd.isHidden = true
    }
    func UnHide(){
        LabelEnd.isHidden = false
    }

    @IBAction func Button1(_ sender: Any) {
        UnHide()

        if (CorrectAnswer == "1"){
            score = score + 1
            scorelabel.text = "Score:\(score)"
            self.performSegue(withIdentifier: "correct", sender: self)
        }
        else {
            LabelEnd.text = "Incorrect Answer! Try again"
        }
    }

    @IBAction func Button2(_ sender: Any) {
        UnHide()

        if (CorrectAnswer == "2"){
            self.performSegue(withIdentifier: "correct", sender: self)
        }
        else {
            LabelEnd.text = "Incorrect Answer! Try again"
        }
    }

    @IBAction func Button3(_ sender: Any) {
        UnHide()

        if (CorrectAnswer == "3"){
            self.performSegue(withIdentifier: "correct", sender: self)
        }
        else {
            LabelEnd.text = "Incorrect Answer! Try again"
        }
    }

    @IBAction func Button4(_ sender: Any) {
        UnHide()

        if (CorrectAnswer == "4"){
            self.performSegue(withIdentifier: "correct", sender: self)
        }
        else {
            LabelEnd.text = "Incorrect Answer! Try again"
        }
    }

}

故事板:

推荐答案

您可以使用 AppDelegate 在多个 Viewcontroller 之间共享数据.您始终可以保存、更新和检索应用程序的 AppDelegate 中定义的数据.这里是我之前使用它的答案有效地使用 Appdelegate.如果您需要 Swift 版本,请告诉我.

You can use AppDelegate to share data between multiple Viewcontrollers. You can always save ,update and retrieve data defined in AppDelegate of your App. Here is my previous ans to use it efficiently to use Appdelegate.Do let me know if you need Swift version .

在您的 AppDelegate 中将您的变量定义为

In your AppDelegate define your variable as

class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
  var points : String? // Declare your object
  .... other methods
}
//You can access the property to save and retrieve your file. You can save 
let app = UIApplication.shared.delegate as! AppDelegate
app.points = "yourString"

 Yo can read properties from any viewcontroller as per your requirement as 

let app = UIApplication.shared.delegate as! AppDelegate
let points = app.points?

您还可以使用 NSNotificationCenter 在多个视图控制器中共享数据.如果您有重新分级的查询,请告诉我.

You can also use NSNotificationCenter to share data in multiple viewcontoller. Do let me know if you have queries regrading implementing the same.

如果您需要在导航时将数据从一个视图控制器传递到另一个视图控制器,您还可以使用故事板 ID 使用视图控制器的实例化,正如@Jeetendra 在他的回答中所解释的那样.

If you need to pass data from one viewcontroller to another while navigation you can also use instantiation of viewcontoller using storyboard id as @Jeetendra explains in his ans.

这篇关于在没有 segue 的多个视图控制器之间共享数据(Swift 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆