I have a drill down tableview application with two tableview controllers and one detail view. The data model is a class with 11 subclasses, one for each section of data. All of the subclass properties are strings or contain an array of strings. There are no empty properties. All of the subclasses have the same properties but data unique to its subclass. My thought was to use the didSelectRow method to obtain an indexpath.row integer that I would use to initialize the corresponding subclass and pass it, via segue, to the second tableview controller which would contain all the data needed for the detail view. I know how to use the segue to pass data collections. However, passing an entire subclass seems to involve a different approach. Not sure how to approach this. Do I need to initialize a class in the second view controller that has identical property types to the subclasses but contains no data? The initialize event in the second table view controller would occur in the viewWillLoad method. My assumption is that someone likely knows a better way.
Asked
Active
Viewed 85 times
0
-
"I know how to use the segue to pass data collections. However, passing an entire subclass seems to involve a different approach." Why? Unless its bloody huge as hell, or really slow to initialize, what's the perceived problem? What is the motivation behind your question in other words. – Gruntcakes Nov 16 '16 at 20:57
-
The drill down app serves to present the NH Rules of Evidence which has 11 sections. There are about 70+ rules. My prior model was a nested dictionary and it worked. I had to initialize the entire dictionary. The dictionary was not slow to initialize. I thought subclassing each of the 11 sections would be a more elegant way to make use of hierarchical data, easier to track, easier to work with and easier to search. I do not program for a living so I am learning as I go. Any thoughts are appreciated. – P.Festus Nov 16 '16 at 21:31
-
Sounds like the class you want to pass is actually just a representation of data? In which case you could investigate going down a different route - move the data class to a Model (as in Model-View-Controller design pattern) and make the model available to both view controllers. (If the first VC needs to pass something to the second VC it can be an indication of what data the second VC needs to access, rather than a complete copy or reference to it and the second VC would access the class/data from the model.) – Gruntcakes Nov 16 '16 at 21:40
-
What do you mean when you write: a representation of data? I have already created a separate file that holds the class and its sub-classes. Is that what you mean by move the data class to a Model? The class is initialized in the 1st VC. From the class I populate an array of 11 section titles that are string types. The didSelectRow method gets me the indexpath that I need to identify and initialize the corresponding subclass. So perhaps I could pass the integer to the second view controller and use it to initialize the needed subclass in a switch statement? – P.Festus Nov 16 '16 at 21:57
-
It'll take to long to try and summarize what a model is, better read about it when you have time using tutorials etc. Your suggestion sounds fine given what your current architecture sounds like it is. – Gruntcakes Nov 16 '16 at 22:22
-
Thank you for your thoughts. – P.Festus Nov 16 '16 at 23:18
1 Answers
0
A standard way to pass data from a View controller to another is to use a delegate. Declare a custom protocol which has the functions and properties you need to access from the second View controller. That way you'll share the class instance with the second view controller. You can implement the protocol in the source view controller and pass a reference of the source view controller to the second view after you perform the segue. You'll set the delegate in the function prepare(for segue: UIStoryboardSegue, sender: Any?) that you should declare in the source view controller. This function will be called after calling performSegue. Hope this will help you.

Sergiob
- 838
- 1
- 13
- 28
-
Are you referring to the following: http://stackoverflow.com/a/26089243/5521145 My other thought was to pass the indexpath.row integer via performSegue to a variable in the second view controller and use it in a switch statement. The matching index would initialize the correct subclass. In any event, this seems like a good opportunity to learn how to use a custom protocol. I'll try it out. thx! – P.Festus Nov 16 '16 at 21:14
-
It's up to you . I think the delegate is more Apple style and, in my opinion, more elegant. But in the end it is all about philosophy – Sergiob Nov 16 '16 at 22:10