When passing data how can I make a variable recognizable to multiple classes?












0















I have a function that passes in a string to determine what class my destination vc will be once I navigate. I'm also passing data to vc regardless of which class vc ends up being cast as. However, I'm getting an expected error of




VAlue of type 'UIViewController?' has no member ....




since vc hasn't been cast as any subclass yet.



func passAndNavigateResultsFor(surveyName: String){

let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc = storyboard.instantiateInitialViewController()

//Determining which class vc will be cast as

if surveyName == "EFDizzinessSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFDizzinessSurveyResults
} else if surveyName == "EFHipArthritisSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFHipArthritisSurveyResults
} else if surveyName == "EFKoosKneeSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFKoosKneeSurveyResults
} else if surveyName == "EFLEFSSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFLEFSSurveyResults
} else if surveyName == "EFQuickDASHSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFQuickDASHSurveyResults
} else if surveyName == "EFNeckDisabilitySurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFNeckDisabilitySurveyResults
}

//Data that I want to pass through regardless of which class vc is cast as

if (firstNameResult != nil) && (lastNameResult != nil) {
self.fullName = self.firstNameResult! + " " + self.lastNameResult!
vc.nameText = self.fullName!
}
if painLevelResult != nil {
vc.painLevelText = String(describing: painLevelResult!)
}
if careSatisfactionResult != nil {
vc.careSatisfactionText = String(describing: careSatisfactionResult!)
}
if rateProgressResult != nil {
vc.rateProgressText = String(describing: rateProgressResult!)
}
if therapyGoalsResult != nil {
vc.therapyGoalsText = String(describing: therapyGoalsResult!)
}
if step1Result != nil {
vc.step1Text = String(describing: step1Result!)
}
if step2Result != nil {
vc.step2Text = String(describing: step2Result!)
}
if step3Result != nil {
vc.step3Text = String(describing: step3Result!)
}
if step4Result != nil {
vc.step4Text = String(describing: step4Result!)
}
}


Is there a way to fix this problem, or should I look at a solution like prepareForSegue instead?










share|improve this question


















  • 3





    If all view controllers contain all properties like painLevelText and step2Text create a base class declaring all properties and make your ....Results classes subclasses of this base class. Consider that all instantiated controllers in the if - else if chain will never be used. You should get a warning about that.

    – vadian
    Nov 24 '18 at 22:01













  • +1 to @vadian comment. Why do you think prepareForSegue would work? It's equivalent to coding a push/pop (actually only a pop) to the VC stack. If you want VC #3 to get a variable that is passed between VC #1 and VC #2, that won't do it.

    – dfd
    Nov 24 '18 at 23:35
















0















I have a function that passes in a string to determine what class my destination vc will be once I navigate. I'm also passing data to vc regardless of which class vc ends up being cast as. However, I'm getting an expected error of




VAlue of type 'UIViewController?' has no member ....




since vc hasn't been cast as any subclass yet.



func passAndNavigateResultsFor(surveyName: String){

let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc = storyboard.instantiateInitialViewController()

//Determining which class vc will be cast as

if surveyName == "EFDizzinessSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFDizzinessSurveyResults
} else if surveyName == "EFHipArthritisSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFHipArthritisSurveyResults
} else if surveyName == "EFKoosKneeSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFKoosKneeSurveyResults
} else if surveyName == "EFLEFSSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFLEFSSurveyResults
} else if surveyName == "EFQuickDASHSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFQuickDASHSurveyResults
} else if surveyName == "EFNeckDisabilitySurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFNeckDisabilitySurveyResults
}

//Data that I want to pass through regardless of which class vc is cast as

if (firstNameResult != nil) && (lastNameResult != nil) {
self.fullName = self.firstNameResult! + " " + self.lastNameResult!
vc.nameText = self.fullName!
}
if painLevelResult != nil {
vc.painLevelText = String(describing: painLevelResult!)
}
if careSatisfactionResult != nil {
vc.careSatisfactionText = String(describing: careSatisfactionResult!)
}
if rateProgressResult != nil {
vc.rateProgressText = String(describing: rateProgressResult!)
}
if therapyGoalsResult != nil {
vc.therapyGoalsText = String(describing: therapyGoalsResult!)
}
if step1Result != nil {
vc.step1Text = String(describing: step1Result!)
}
if step2Result != nil {
vc.step2Text = String(describing: step2Result!)
}
if step3Result != nil {
vc.step3Text = String(describing: step3Result!)
}
if step4Result != nil {
vc.step4Text = String(describing: step4Result!)
}
}


Is there a way to fix this problem, or should I look at a solution like prepareForSegue instead?










share|improve this question


















  • 3





    If all view controllers contain all properties like painLevelText and step2Text create a base class declaring all properties and make your ....Results classes subclasses of this base class. Consider that all instantiated controllers in the if - else if chain will never be used. You should get a warning about that.

    – vadian
    Nov 24 '18 at 22:01













  • +1 to @vadian comment. Why do you think prepareForSegue would work? It's equivalent to coding a push/pop (actually only a pop) to the VC stack. If you want VC #3 to get a variable that is passed between VC #1 and VC #2, that won't do it.

    – dfd
    Nov 24 '18 at 23:35














0












0








0








I have a function that passes in a string to determine what class my destination vc will be once I navigate. I'm also passing data to vc regardless of which class vc ends up being cast as. However, I'm getting an expected error of




VAlue of type 'UIViewController?' has no member ....




since vc hasn't been cast as any subclass yet.



func passAndNavigateResultsFor(surveyName: String){

let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc = storyboard.instantiateInitialViewController()

//Determining which class vc will be cast as

if surveyName == "EFDizzinessSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFDizzinessSurveyResults
} else if surveyName == "EFHipArthritisSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFHipArthritisSurveyResults
} else if surveyName == "EFKoosKneeSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFKoosKneeSurveyResults
} else if surveyName == "EFLEFSSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFLEFSSurveyResults
} else if surveyName == "EFQuickDASHSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFQuickDASHSurveyResults
} else if surveyName == "EFNeckDisabilitySurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFNeckDisabilitySurveyResults
}

//Data that I want to pass through regardless of which class vc is cast as

if (firstNameResult != nil) && (lastNameResult != nil) {
self.fullName = self.firstNameResult! + " " + self.lastNameResult!
vc.nameText = self.fullName!
}
if painLevelResult != nil {
vc.painLevelText = String(describing: painLevelResult!)
}
if careSatisfactionResult != nil {
vc.careSatisfactionText = String(describing: careSatisfactionResult!)
}
if rateProgressResult != nil {
vc.rateProgressText = String(describing: rateProgressResult!)
}
if therapyGoalsResult != nil {
vc.therapyGoalsText = String(describing: therapyGoalsResult!)
}
if step1Result != nil {
vc.step1Text = String(describing: step1Result!)
}
if step2Result != nil {
vc.step2Text = String(describing: step2Result!)
}
if step3Result != nil {
vc.step3Text = String(describing: step3Result!)
}
if step4Result != nil {
vc.step4Text = String(describing: step4Result!)
}
}


Is there a way to fix this problem, or should I look at a solution like prepareForSegue instead?










share|improve this question














I have a function that passes in a string to determine what class my destination vc will be once I navigate. I'm also passing data to vc regardless of which class vc ends up being cast as. However, I'm getting an expected error of




VAlue of type 'UIViewController?' has no member ....




since vc hasn't been cast as any subclass yet.



func passAndNavigateResultsFor(surveyName: String){

let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc = storyboard.instantiateInitialViewController()

//Determining which class vc will be cast as

if surveyName == "EFDizzinessSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFDizzinessSurveyResults
} else if surveyName == "EFHipArthritisSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFHipArthritisSurveyResults
} else if surveyName == "EFKoosKneeSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFKoosKneeSurveyResults
} else if surveyName == "EFLEFSSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFLEFSSurveyResults
} else if surveyName == "EFQuickDASHSurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFQuickDASHSurveyResults
} else if surveyName == "EFNeckDisabilitySurveyTask" {
let vc = storyboard.instantiateViewController(withIdentifier :surveyName) as! EFNeckDisabilitySurveyResults
}

//Data that I want to pass through regardless of which class vc is cast as

if (firstNameResult != nil) && (lastNameResult != nil) {
self.fullName = self.firstNameResult! + " " + self.lastNameResult!
vc.nameText = self.fullName!
}
if painLevelResult != nil {
vc.painLevelText = String(describing: painLevelResult!)
}
if careSatisfactionResult != nil {
vc.careSatisfactionText = String(describing: careSatisfactionResult!)
}
if rateProgressResult != nil {
vc.rateProgressText = String(describing: rateProgressResult!)
}
if therapyGoalsResult != nil {
vc.therapyGoalsText = String(describing: therapyGoalsResult!)
}
if step1Result != nil {
vc.step1Text = String(describing: step1Result!)
}
if step2Result != nil {
vc.step2Text = String(describing: step2Result!)
}
if step3Result != nil {
vc.step3Text = String(describing: step3Result!)
}
if step4Result != nil {
vc.step4Text = String(describing: step4Result!)
}
}


Is there a way to fix this problem, or should I look at a solution like prepareForSegue instead?







ios swift






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 21:39









Austin BerenyiAustin Berenyi

14510




14510








  • 3





    If all view controllers contain all properties like painLevelText and step2Text create a base class declaring all properties and make your ....Results classes subclasses of this base class. Consider that all instantiated controllers in the if - else if chain will never be used. You should get a warning about that.

    – vadian
    Nov 24 '18 at 22:01













  • +1 to @vadian comment. Why do you think prepareForSegue would work? It's equivalent to coding a push/pop (actually only a pop) to the VC stack. If you want VC #3 to get a variable that is passed between VC #1 and VC #2, that won't do it.

    – dfd
    Nov 24 '18 at 23:35














  • 3





    If all view controllers contain all properties like painLevelText and step2Text create a base class declaring all properties and make your ....Results classes subclasses of this base class. Consider that all instantiated controllers in the if - else if chain will never be used. You should get a warning about that.

    – vadian
    Nov 24 '18 at 22:01













  • +1 to @vadian comment. Why do you think prepareForSegue would work? It's equivalent to coding a push/pop (actually only a pop) to the VC stack. If you want VC #3 to get a variable that is passed between VC #1 and VC #2, that won't do it.

    – dfd
    Nov 24 '18 at 23:35








3




3





If all view controllers contain all properties like painLevelText and step2Text create a base class declaring all properties and make your ....Results classes subclasses of this base class. Consider that all instantiated controllers in the if - else if chain will never be used. You should get a warning about that.

– vadian
Nov 24 '18 at 22:01







If all view controllers contain all properties like painLevelText and step2Text create a base class declaring all properties and make your ....Results classes subclasses of this base class. Consider that all instantiated controllers in the if - else if chain will never be used. You should get a warning about that.

– vadian
Nov 24 '18 at 22:01















+1 to @vadian comment. Why do you think prepareForSegue would work? It's equivalent to coding a push/pop (actually only a pop) to the VC stack. If you want VC #3 to get a variable that is passed between VC #1 and VC #2, that won't do it.

– dfd
Nov 24 '18 at 23:35





+1 to @vadian comment. Why do you think prepareForSegue would work? It's equivalent to coding a push/pop (actually only a pop) to the VC stack. If you want VC #3 to get a variable that is passed between VC #1 and VC #2, that won't do it.

– dfd
Nov 24 '18 at 23:35












1 Answer
1






active

oldest

votes


















0














What is the exact error being thrown? What member does it not exactly have?



Personally I agree with you and would use prepare for segue like this:



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let yourVC = segue.destination as? YourViewController {
yourVC.painLevelText = painLevelText
}
}


You could find, cast, and prepare the correct VC in there.



If you are having problems with properties not found then you should consider creating a superclass for your VC's to store properties like painLevelText






share|improve this answer


























  • @dfd the code looks like OP is trying to assign the type of ViewController at instantiation and then access a property. Prepare for segue offers a better solution instead of accessing the properties after instantiation. I cannot confirm if those properties were already created but I included it for clarification, thanks

    – cevalloa
    Nov 25 '18 at 0:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462599%2fwhen-passing-data-how-can-i-make-a-variable-recognizable-to-multiple-classes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














What is the exact error being thrown? What member does it not exactly have?



Personally I agree with you and would use prepare for segue like this:



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let yourVC = segue.destination as? YourViewController {
yourVC.painLevelText = painLevelText
}
}


You could find, cast, and prepare the correct VC in there.



If you are having problems with properties not found then you should consider creating a superclass for your VC's to store properties like painLevelText






share|improve this answer


























  • @dfd the code looks like OP is trying to assign the type of ViewController at instantiation and then access a property. Prepare for segue offers a better solution instead of accessing the properties after instantiation. I cannot confirm if those properties were already created but I included it for clarification, thanks

    – cevalloa
    Nov 25 '18 at 0:08
















0














What is the exact error being thrown? What member does it not exactly have?



Personally I agree with you and would use prepare for segue like this:



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let yourVC = segue.destination as? YourViewController {
yourVC.painLevelText = painLevelText
}
}


You could find, cast, and prepare the correct VC in there.



If you are having problems with properties not found then you should consider creating a superclass for your VC's to store properties like painLevelText






share|improve this answer


























  • @dfd the code looks like OP is trying to assign the type of ViewController at instantiation and then access a property. Prepare for segue offers a better solution instead of accessing the properties after instantiation. I cannot confirm if those properties were already created but I included it for clarification, thanks

    – cevalloa
    Nov 25 '18 at 0:08














0












0








0







What is the exact error being thrown? What member does it not exactly have?



Personally I agree with you and would use prepare for segue like this:



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let yourVC = segue.destination as? YourViewController {
yourVC.painLevelText = painLevelText
}
}


You could find, cast, and prepare the correct VC in there.



If you are having problems with properties not found then you should consider creating a superclass for your VC's to store properties like painLevelText






share|improve this answer















What is the exact error being thrown? What member does it not exactly have?



Personally I agree with you and would use prepare for segue like this:



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let yourVC = segue.destination as? YourViewController {
yourVC.painLevelText = painLevelText
}
}


You could find, cast, and prepare the correct VC in there.



If you are having problems with properties not found then you should consider creating a superclass for your VC's to store properties like painLevelText







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 23:59

























answered Nov 24 '18 at 21:52









cevalloacevalloa

1024




1024













  • @dfd the code looks like OP is trying to assign the type of ViewController at instantiation and then access a property. Prepare for segue offers a better solution instead of accessing the properties after instantiation. I cannot confirm if those properties were already created but I included it for clarification, thanks

    – cevalloa
    Nov 25 '18 at 0:08



















  • @dfd the code looks like OP is trying to assign the type of ViewController at instantiation and then access a property. Prepare for segue offers a better solution instead of accessing the properties after instantiation. I cannot confirm if those properties were already created but I included it for clarification, thanks

    – cevalloa
    Nov 25 '18 at 0:08

















@dfd the code looks like OP is trying to assign the type of ViewController at instantiation and then access a property. Prepare for segue offers a better solution instead of accessing the properties after instantiation. I cannot confirm if those properties were already created but I included it for clarification, thanks

– cevalloa
Nov 25 '18 at 0:08





@dfd the code looks like OP is trying to assign the type of ViewController at instantiation and then access a property. Prepare for segue offers a better solution instead of accessing the properties after instantiation. I cannot confirm if those properties were already created but I included it for clarification, thanks

– cevalloa
Nov 25 '18 at 0:08




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462599%2fwhen-passing-data-how-can-i-make-a-variable-recognizable-to-multiple-classes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python