Ios 不使用segue在多个视图控制器之间共享数据(Swift 3)

Ios 不使用segue在多个视图控制器之间共享数据(Swift 3),ios,swift,xcode,uinavigationcontroller,Ios,Swift,Xcode,Uinavigationcontroller,我正在做一个二维码扫描器测试应用程序,用户在做10个问题时必须得到10分。用户输入第一个qn后,分数将加1,然后返回qr扫描页面,用户必须在该页面上扫描下一个qn的qr码。问题在于分数数据的传递。有没有一种不用segue的方法 这是我的QN1控制器 import UIKit class Quiz1Controller: UIViewController { @IBOutlet var question: UILabel! @IBOutlet var button1: UIBu

我正在做一个二维码扫描器测试应用程序,用户在做10个问题时必须得到10分。用户输入第一个qn后,分数将加1,然后返回qr扫描页面,用户必须在该页面上扫描下一个qn的qr码。问题在于分数数据的传递。有没有一种不用segue的方法

这是我的QN1控制器

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"
        }
    }

}
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"
        }
    }

}
这是我的qn2控制器

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"
        }
    }

}
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"
        }
    }

}
故事板:


删除Seague并按如下方式执行。假设您想要共享
NSMutableDictionary

只需将一个变量作为要与viewController共享的类型

比如说。您希望与ShareViewController共享数据,然后使用如下变量

var Dict_data = NSMutableDictionary()
现在,您想导航到哪里,只需假设它是您的ShareViewController。记住不要忘记在情节提要中提供ShareViewController的标识符

    let PV = self.storyboard?.instantiateViewController(withIdentifier: "ShareViewController") as! ShareViewController
    PV.data = dict_share_date 
    self.navigationController?.pushViewController(PV, animated: true)

这里dict_share_数据是您的
nsmutabledictionary
。您可以选择任何类型,但请记住,两侧类型必须相同。或者你也可以进行类型转换。

删除Seague并按如下方式进行。假设您想要共享
NSMutableDictionary

只需将一个变量作为要与viewController共享的类型

比如说。您希望与ShareViewController共享数据,然后使用如下变量

var Dict_data = NSMutableDictionary()
现在,您想导航到哪里,只需假设它是您的ShareViewController。记住不要忘记在情节提要中提供ShareViewController的标识符

    let PV = self.storyboard?.instantiateViewController(withIdentifier: "ShareViewController") as! ShareViewController
    PV.data = dict_share_date 
    self.navigationController?.pushViewController(PV, animated: true)

这里dict_share_数据是您的
nsmutabledictionary
。您可以选择任何类型,但请记住,两侧类型必须相同。或者您可以进行类型转换。

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

在AppDelegate中,将变量定义为

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在多个ViewController中共享数据。如果您有相同的查询,请务必告诉我


如果您需要在导航时将数据从一个viewcontroller传递到另一个viewcontroller,您还可以使用脚本id实例化viewcontroller,正如@Jeetendra在其ans中所解释的那样。

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

在AppDelegate中,将变量定义为

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在多个ViewController中共享数据。如果您有相同的查询,请务必告诉我



如果您需要在导航时将数据从一个viewcontroller传递到另一个viewcontroller,您还可以使用脚本id实例化viewcontroller,正如@Jeetendra在其ans中所解释的那样。

考虑以模式呈现扫描仪视图控制器。然后,您可以获得对演示控制器的引用,并可以将数据传回。如果需要传递数据,可以使用委托或通知。如果你需要共享数据,你可以使用SuntLon。你可以使用委托方法和NSuSerDebug来在App中共享你的数据。你可以使用委托方法和NSuSerDebug来共享整个应用程序中的数据。然后,您可以获得对演示控制器的引用,并可以将数据传回。如果需要传递数据,可以使用委托或通知。如果您需要共享数据,您可以使用singleton。您可以使用delegate方法和NSUserDefaults在应用程序中共享整个数据。您可以使用delegate方法和NSUserDefaults在应用程序中共享整个数据。您好,如果您能发送一个swift版本,或者甚至是一个循序渐进的教程,我将不胜感激,因为我是swift/Xcode新手,这是我的主要项目:(你需要swift版本的Appdelegate appraoch??是的,我认为nsnotificationcenter也适用于我的,但你所说的重新分级执行相同的查询是什么意思?我已经编辑了我的ans,以包括swift版本。我的上述评论是指,如果需要有关NSNotification Center执行的任何信息,我将帮助你。)u、 因为我的分数是一个整数,所以在appdelegate中,我是否应该把它写为“var score:int?”?您好,如果您能发送一个swift版本,或者甚至是一个分步教程,我将不胜感激,因为我是swift/Xcode新手,这是我的主要项目:(你需要swift版本的Appdelegate appraoch??是的,我认为nsnotificationcenter也适用于我的,但你所说的重新分级执行相同的查询是什么意思?我已经编辑了我的ans,以包括swift版本。我的上述评论是指,如果需要有关NSNotification Center执行的任何信息,我将帮助你。)u、 因为我的分数是一个整数,所以在appdelegate中我应该把它作为“var分数:int”吗?