When passing data how can I make a variable recognizable to multiple classes?
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
add a comment |
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
3
If all view controllers contain all properties likepainLevelText
andstep2Text
create a base class declaring all properties and make your....Results
classes subclasses of this base class. Consider that all instantiated controllers in theif - 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 thinkprepareForSegue
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
add a comment |
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
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
ios swift
asked Nov 24 '18 at 21:39
Austin BerenyiAustin Berenyi
14510
14510
3
If all view controllers contain all properties likepainLevelText
andstep2Text
create a base class declaring all properties and make your....Results
classes subclasses of this base class. Consider that all instantiated controllers in theif - 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 thinkprepareForSegue
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
add a comment |
3
If all view controllers contain all properties likepainLevelText
andstep2Text
create a base class declaring all properties and make your....Results
classes subclasses of this base class. Consider that all instantiated controllers in theif - 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 thinkprepareForSegue
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
add a comment |
1 Answer
1
active
oldest
votes
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
@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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
@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
add a comment |
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
@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
add a comment |
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
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
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
add a comment |
@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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
If all view controllers contain all properties like
painLevelText
andstep2Text
create a base class declaring all properties and make your....Results
classes subclasses of this base class. Consider that all instantiated controllers in theif - 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