Passing Data Between ViewControllers not working

Text

Answered by Claude31 in 399243022

You are mixing between the VC.


In VC A (is it OverviewController), declare:

@IBOutlet weak var gradeValue: NSTextField! // data input field
var nameGrade: String = "" //data variable

No need to create nameGrade. But let's do it to show


In VC B (I understand it is FiguresViewController)

declare

var passedFromA: String = "" //data variable



in VC A, in prepare, you will pass the content of this textField


override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
        if let vc = segue.destinationController as? FiguresViewController {
            // THIS is wrong, not needed OverviewController.value = nameGrade //ERROR Ambiguous use of value
            nameGrade = gradeValue.text     // You store the content of textField into this VC A property
            vc.passedFromA = nameGrade // passedFromA is defined in VC B ; = "passedFromA"//ERROR Value of type 'FiguresViewController' has no member 'nameGrade'
        }
    }



Now, in VC B (FiguresViewController), in viewDidLoad

if let  passedValue = passedFromA {     //  passedFromA is optional, need to test for nil
     switch passedValue:
     case 1 : // select plist1
     case 2 : // select plist 2
     default : // select default plist
}



Hope that's clear

Hard to help on this ! 😁 🙂

I have an @IBOutlet on View Controller A where the user enters a number (grade assignment). I also have a button on View Controller A that segues to View Controller B and runs the code to display information in a .plist file in a popup button where the user can select items.


I want to be able to select a different .plist file depending on what the user enters in @IBOutlet on ViewController A.


I can setup the If .. else code in ViewController B to determine which .plist file to run but I need to get the information entered in the @IBOutlet from ViewController A to ViewController B.


I don't know, how even though I've read several explanations.


Any Help will be appreciated.

That's a pretty classical scheme.


In controller B, declare:

var passedFromA : Int?

That will be the vehivcle to pass data.


In Controller A, you have defined a prepare for segue.


Set the value there from the grade you have entered:

destVC.passedFromA = definedGrade     // destVC is of type Controller B of course)


Finally, in Controller B:

if let  passed = passedFromA {
     switch passed:
     case 1 : // select plist1
     case 2 : // select plist 2
     default : // select default plist 
}



Maybe you could show the code that is not working.

I have the following in VC A:


@IBOutlet weak var gradeValue: NSTextField! // data input field


var nameGrade: String = "" //data variable


Here is what I have done trying to implement your suggestion: But with Errors because I am not implementing correctly:


In VC B:


var passedFromA: String?


In VC A:


overridefunc prepare(for segue: NSStoryboardSegue, sender: Any?) {
        if let vc = segue.destinationController as? FiguresViewController {
            OverviewController.value = nameGrade //ERROR Ambiguous use of value
            vc.nameGrade = "passedFromA"//ERROR Value of type 'FiguresViewController' has no member 'nameGrade'
        }
    }
Accepted Answer

You are mixing between the VC.


In VC A (is it OverviewController), declare:

@IBOutlet weak var gradeValue: NSTextField! // data input field
var nameGrade: String = "" //data variable

No need to create nameGrade. But let's do it to show


In VC B (I understand it is FiguresViewController)

declare

var passedFromA: String = "" //data variable



in VC A, in prepare, you will pass the content of this textField


override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
        if let vc = segue.destinationController as? FiguresViewController {
            // THIS is wrong, not needed OverviewController.value = nameGrade //ERROR Ambiguous use of value
            nameGrade = gradeValue.text     // You store the content of textField into this VC A property
            vc.passedFromA = nameGrade // passedFromA is defined in VC B ; = "passedFromA"//ERROR Value of type 'FiguresViewController' has no member 'nameGrade'
        }
    }



Now, in VC B (FiguresViewController), in viewDidLoad

if let  passedValue = passedFromA {     //  passedFromA is optional, need to test for nil
     switch passedValue:
     case 1 : // select plist1
     case 2 : // select plist 2
     default : // select default plist
}



Hope that's clear

Thanks Claude31. That works!

Passing Data Between ViewControllers not working
 
 
Q