0

I am creating an app that has 2 View Controllers, and I am passing data using preparForSegue. I managed to pass data to variables that are declared in the second view which has a TableView. Here is what I have:

override func viewDidLoad() {
   super.viewDidLoad()
     }

var cprCertification : String!
var cprExpiry : String!
var scipCertification : String!
var scipExpiry : String!
var diabCertification : String!
var diabExpiry : String!

The variables above hold data passed from previous View Controller.

Below is the tuple that I would like to insert those variables in.

let training = [("CPR/First Aid", cprCertification, cprExpiry), ("SCIP", scipCertification, scipExpiry), ("Diabetes", diabCertification, diabExpiry)]

I get errors saying the controller does not have member named: cprCertification, etc. If I include the tuple training in viewDidLoad(), all those variables are recognizable; however, I get errors about Tableview not recognizing training. I do not know what I am missing. I thought about placing the tuple in viewDidLoad() and access it somehow so it can be implemented in the rest of the code, but I am unsure if it can be done and how. Or if there are other suggestions or corrections, I would greatly appreciate it!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
RochNoure
  • 77
  • 1
  • 6
  • it may help if you expose those variables to the app as if they are "properties" of the view controller as is done with ObjC, so this means after you "class declaration" in the view controller and before all other declarations in the view controller, this should expose these variables to the previous view controller where if can then "set" these variables. And, in all reality, perhaps a more straightforward method is to create a custom initiliazer and pass variables through the init methods of the view controller – Larry Pickles Aug 16 '15 at 04:58
  • Thank you for quick reply. Pardon my ignorance but how do you do the first? Do you declare it as public? – RochNoure Aug 16 '15 at 05:16
  • I know how to do this in objective c with robust setting and and getting with instance variables and properties, that's why I say the second way is probably easier for you, just google or search here on stack "custom initializers swift" and this shoudl help you out, it's basically this: when you call to the NEXT view controller, in your actuall call method you send the tuple, and then you can use them in the NEXT view controller without too much of an issue – Larry Pickles Aug 16 '15 at 05:18
  • here's some info on setting and getting like the first method i explained: http://blog.johnregner.com/post/103092814578/swift-properties-custom-setters-and-getters – Larry Pickles Aug 16 '15 at 05:20
  • and for the second part, see these: http://www.learnswift.io/blog/2014/6/4/custom-initializers-in-swift https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html – Larry Pickles Aug 16 '15 at 05:23
  • Ok, I will look into that, thank you very much!! – RochNoure Aug 16 '15 at 05:24
  • Oh yeah, here's this too: http://stackoverflow.com/questions/29992318/pass-custom-class-data-between-view-controllers-in-swift – Larry Pickles Aug 16 '15 at 05:26
  • Hello, something came up. The previous Viewcontroller does not recognize those variables. So I went ahead and declared those variables as I initially did. Can I attach the two images so you can see what's going on? – RochNoure Aug 20 '15 at 06:02
  • yep, go ahead and show them – Larry Pickles Aug 20 '15 at 06:03
  • http://imgur.com/9CDcLqz,GoyZb0y#1 – RochNoure Aug 20 '15 at 06:13
  • yep, the reason why is because that "training" is only declared locally to the viewDidLoad, you will have to declare it above to be available to the entire view controller and then you can define it in view did load if you wish – Larry Pickles Aug 20 '15 at 06:16
  • something like this, where you declare the other variables "var training = NSDictionary()" – Larry Pickles Aug 20 '15 at 06:19
  • I did that and I got an error saying that ViewController.Type does not have a member named "cprCert...etc." (all variables). From what I have learned, the data I'm retrieving from the previous controller through the variables declared above can only work if included within viewDidLoad(), that's why I put the training tuple in there. – RochNoure Aug 20 '15 at 06:20
  • well, then what about this: keep it in the viewdidLoad and then create another variable above and then in the viewdidload, assign the new variable the value of the training variable such that you change the other variables to the vlaue of the new variable – Larry Pickles Aug 20 '15 at 06:22
  • var something = NSDictionary() <<=== or however you declare a "tuple", then in viewdidLoad, "training = blah blah blah <<== what you are already using" and then "something = training" and then for the stuf in red, do this "return something.count" – Larry Pickles Aug 20 '15 at 06:23
  • Ok, let me try that and I will get back to you. Thanks!! – RochNoure Aug 20 '15 at 06:29
  • Actually, here's how you declare the tuple to store the value for the training tuple: "var tupleToStoreTrianingTuple :[(String,String!,String!)] = [] " this should work, just declare this underneath all the strings that you declared above – Larry Pickles Aug 20 '15 at 06:35
  • I was just going to ask, because I got an error that says: cannot assign a value of type string, string, string, to a value of type NSDictionary() . Thanks. Let me try that. – RochNoure Aug 20 '15 at 06:47
  • Now, it's recognizable. Thanks! There is one more, hopefully last, error. Check the image: http://i.imgur.com/Ap2NRzF.png – RochNoure Aug 20 '15 at 06:58
  • If I comment it, I get "subtitle" in tableview. – RochNoure Aug 20 '15 at 06:59
  • it should be || "Ex ... blah blah, you are missing a quotation mark – Larry Pickles Aug 20 '15 at 07:01
  • Nope, it's there, before || – RochNoure Aug 20 '15 at 07:03
  • so, you literally want your cell text to say something like this "|| something something" that's what your logic shows – Larry Pickles Aug 20 '15 at 07:03
  • i think you want + || + "Ex blah blah... – Larry Pickles Aug 20 '15 at 07:04
  • asa in certificationDate) || ("Expir blah blah .. you need to show your intent iwth the If/then line operation you are trying to perform unless you really really want the "||" in your string – Larry Pickles Aug 20 '15 at 07:06
  • Yes, exactly. I tested it before loading data from previous ViewController and it worked. the || does not have to be there, just for formatting reasons. It shows something like this: Certification Date: 07-06-2014 || Expiry Date: 07-06-2016 – RochNoure Aug 20 '15 at 07:08
  • i see, one sec, let me check something – Larry Pickles Aug 20 '15 at 07:11
  • let me send an image with local data loaded in tableview – RochNoure Aug 20 '15 at 07:12
  • what i would try is this: certificationDate +"|"+"|"+"Ex .. blah blah, .. just in case the string concatenation in Swift is being dumb and thinks that "||" is an OR operator – Larry Pickles Aug 20 '15 at 07:14
  • That makes sense but it works with local data. Take a look and let me know what you think I am missing. (2 images) http://imgur.com/y4KNNZ8,DX5dzSV#0 – RochNoure Aug 20 '15 at 07:16
  • yeap, I'm in playground and the "||" works fine like you have it, one sec – Larry Pickles Aug 20 '15 at 07:17
  • That image looks like its correct? What am I missing? – Larry Pickles Aug 20 '15 at 07:17
  • You are not missing anything. The thing is this works fine with local data, look at "training", I have manually included the dates instead of loading them. I just tested everything else in the previous ViewController, and it seems to be working fine. – RochNoure Aug 20 '15 at 07:21
  • ahh, i see, I think the "String!" stuff we defined in the Tuple above MAY not be storing an actual "String!" so it's failing, see this http://stackoverflow.com/questions/28804654/what-does-error-thread-1exc-bad-instruction-code-exc-i386-invop-subcode-0x0 ... and search on StackOverflow for that error adn you'll find a lot answers – Larry Pickles Aug 20 '15 at 07:22
  • But, when I try to load the data dynamically (from previous controller) it shows the error as I sent eariler – RochNoure Aug 20 '15 at 07:23
  • Ok. Thank you so much for your help!! – RochNoure Aug 20 '15 at 07:25
  • I will post back once it's resolved. – RochNoure Aug 20 '15 at 07:28
  • no problem, good luck – Larry Pickles Aug 20 '15 at 07:29
  • Hello again, I've been ruling out where the error could be coming from. Take a look at this image http://i.imgur.com/h7fkFQd.png. The data retrieved works 100%. I know this because I created a label on the first view controller and it did show the result. I've commented out the label and proceeded to prepareForSegeue. I created a new view controller with labels only to ensure data is passed before I mess with TableViews. When I run it the second view shows nothing, the default name of label is not even there, so it looks like it is passing "something". Not sure what I am missing here! – RochNoure Aug 21 '15 at 06:09
  • first thing I see is the only ! variable that you are passing is the first one, like this: cprCertJsonTJ! ... and the rest are passed without this, which I don't know if it causes a problem, I barely know swift, but I'd sort this out first – Larry Pickles Aug 21 '15 at 06:15
  • I did only one jut to test. I run it in different ways; I passed all variables, I deleted all but one, I commented them out, still same problem. If the data was not passed, the label would say "Label" or whatever default name assigned to it, but in this case the label itself is gone, I run it and I get blank screen. Maybe I should look into defining String differently? – RochNoure Aug 21 '15 at 06:20
  • Yeah, I'd try all variable combos before you give up on it, it's usually something simple-tricky like that – Larry Pickles Aug 21 '15 at 06:21
  • Also, have you done a "println()" on the the variables you are passing beore you pass them? I'd try to print them all out to make sure they are getting the exact reutls from the JSON, even if you have already verified the json – Larry Pickles Aug 21 '15 at 06:22
  • I did that. I also did the same with Label and it did show the result. I tried passing local data from one view controller to another with the exact same code and it worked. Look at the code again, if I uncomment the label, which is above prepareForSegue function , it shows the result parsed. Is local data different from parsed and should it be dealt with differently is what I think I should look into. – RochNoure Aug 21 '15 at 06:35
  • well, the local data, as in if these variables are local to the view controller only could cause an issue, I'd have to see more code – Larry Pickles Aug 21 '15 at 06:40
  • Problem solved. I followed the same thing you told me to do for the tableView. I moved the prepareForSegue function outside viewDidLoad(). Then I declared a new String variable above everything. After that, I called it to receive the data retrieved through JSON, then I implemented it in the prepareForSegue. I guess the function did not work while it was inside the viewDidLoad(), but I wouldn't have figured it out, hadn't you you showed me the trick! I thank you for your time and help! – RochNoure Aug 21 '15 at 08:12
  • no problem, Im glad it worked! – Larry Pickles Aug 21 '15 at 08:15

0 Answers0