Data based on previous viewController, Class or rootViewController - iphone

I have 4 seperate tableViews which, when selected do a modal segue to a tabBarController containing four tabs. Of those four tabs they all have a common tab which performs an XML parse.
Ive done some reading and it seems I have three ways of doing it.Either I can
A) Use a class to define where the data is coming from but I don't want to instantiate all the options
B) Find out what the current TabBarController is and use that as a condition
c) Find out where we segued from and use that data
Which would be the best way to perform this action, and how would I do it(the segue is working with data being passed). Please note that I am not using the UINavigationController.
I basically want to end up with a switch performing the functions based on where its coming from.

Well, you could use a singleton.
You could also create a data model and pass pointers to it to your various classes, as needed.
As for using a switch, just setup your data with an index of some sort, such as an array, and pull it based on the current tab bar object. If you plan to use UITabBarController then call its selectedIndex property to know which tab bar item is selected.

Related

Swift Communicating between view controllers

I'm new to Swift and was wondering how I could set up segues between three view controllers. What I'm thinking is that the first viewController allows the user to choose between 2 objects, than redirects to secondView which allows to choose between 22 objects and based on the combination of the user's past 2 view controller choices, a new tableView is created. How can I trace the user's decisions and provide a tableView related to that in such a program? Thank you very much in advance.
I have Two rough solution for you.
(Not recommended) use two global variables VcOne and VcTwo. while selecting first object put a value in VcOne and when selecting from second 22 objects put value in VcTwo. and in tableView's Controller check both the variables and create your table View.
While performing Segue, have a Variable in destination ViewController and put a identifier. and have two variables on the Third ViewController. when you perform segue from second ViewController, assign Identifier of previous ViewController and current View Controller to next controller's Variables. and in tableView's Controller check both the variables and create your table View.
You can also use a array to keep track of user's selection.
Those are very basics of iOS development. just split the tasks that you need to do.
First split steps you have to do. First study about the navigation between View Controllers. in your case using segue. There are lots of questions regarding that in stack overflow.
then find out how to pass data between viewControllers. once you know about navigation it is very easy.
You can create logic with the values you are passing recieving.
ex: firstViewController will pass parameter called objectCount to secondViewcontroller. and based on that value count you can decide what are objects should show hidden.
And then based on that selection you need to get Object back to the firstViewController or go to another 3rd View Controller.
I'm really sorry but I don't think you can start developing iPhone application without understanding the basics.
You will need three ViewControllers and you need to use parepare(for:sender:) method to pass the data from one ViewController to another ViewController.
Here is an example on how to pass data between ViewControllers.

"Reuse" UITableViewController in UITabBarController

So I started to work on an app, it has 5 tabs of which 3 shows UITableViewController with custom cells, but with different data.
Is it possible to link 3 tabs to same UITableViewController, but check which tab is selected, and according to that, load right data?
I tried putting in UITableViewController:
if (self.tabBarController.selectedIndex == 0)
//Load array1
else if (self.tabBarController.selectedIndex == 1)
//Load array2
else
//Load array3
But the indexes are not always the same (it depends in which order you select tabs?, and sometimes I get index that is very high number).
How would you do this?
Assuming you mean "Is it possible to link 3 tabs to same UITableViewController instance":
It is possible, but it's not a great idea. I believe (but couldn't point you at chapter and verse) that the assumption by Apple is that different tabs will have unique, different view controller instances in them. When you change tabs, the view controller instance about to hide has various 'about to be hidden' lifecycle methods called on it. Likewise, the view controller instance about to appear has various 'about to be shown' lifecycle methods shown on it. It's possible your single view controller instance may fall foul of the order of these lifecycle methods being shown.
The above point aside, it's also a waste of effort reloading a table each time just because the user changed a tab. Be kind and do things the way you're meant to do them. There's no reason you can't have a single view controller class that handles everything, but you create a different instance of it for each tab.
Note: your UIViewController being aware of which tab it is in counts as a 'code smell'. This usually means you're doing something in an undesirable fashion and hurting the re-usability of this code. That applies in this case.
To avoid this 'code smell', your view controller should be able to be given the data it is to show, without any knowledge of even being shown in a UITabBarController.
Leverage the UITabBarControllerDelegate to detect when the user switches tabs.
Upon tab switch:
assign the new array contents to a member variable used to display
the data in the table view (with a similar if/else block as you posted), and
call reloadData on your UITableView
to trigger a redraw with the data

how to pass variables between a complex navigational stack

I have several uitableviews that the user can use to set different types of search parameters in my application, I will allow the user to select a cell that will then push a new view onto the stack in which the user can choose what to search and then return back to the original view with the value passed into the originally selected cell..
Where this gets complicated is that each of the original search views will be able to push the same view for search results onto the stack..
here is a graphical example of what I mean as it is hard to explain.
In this example only one viewcontroller can show at a time when a cell in the current view controller is selected the subview is loaded with the list of options, both view controllers use the same subviewcontroller.
My question is to do with returning values that you select in the subviewcontroller back to the correct viewcontroller...
I am trying to figure out how to return the selected value in the subviewcontroller to the correct cell in the correct viewcontroller.. I am hoping to get some suggestions on the correct way to handle this type of view structure as its not really covered in many books that I have available to me, plus I hope to draw on your guys knowledge and experience with similar projects.
One common way is to define a protocol in the sub view controller for callbacks, and have each of the main view controllers implement that... when they create or call up the sub view controller, they tell it that they are the selection delegate to be called when a change is made. Then they can also choose to dismiss the sub view controller.

How to use a tableView as a selector to send values to another viewcontroller?

I'll try to explain this as best as i can, but i appologize if it gets too confusing - I've been stuck on this problem for many hours now.
In my application i have a search screen where the user will be able to select a bunch of criterias to perform the search by. Some of these criterias consists of fairly long lists of values to choose from and therefor i want a tableview on my searchscreen which have 4 rows - Each row representing a criteria that the user can set.
Once the user clicks on a row i want to push a new window in my navigationcontroller which consists of a new table containing the selectable values for that criteria - Once the user clicks on one of these rows on the new window, i want the selected value to be sent back to my main searchscreen and pop back to my search screen.
What would be the best way to do this?
Elaboration:
My searchscreen is called SearchViewController and is contained in a navigationController.
SearchViewController contains two sliders and a tableView with 4 rows called "Searchtype", "Property type", "Salestype", "Area" and a searchbutton. If the user clicks on "Searchtype" then i want a new view to be pushed in the navigationController which should contain a new tableView with a bunch of rows representing different possible values for the "Searchtype" criteria - The same goes for all 4 rows in SearchViewController.
When the user clicks on one of the value rows in the newly pushed tableView i want that tableView to be popped away and the selected value sent back to the SearchViewController allowing the user to either select more criterias or push the search button to actually perform a search based on the selected criterias.
But i can't figure out the best way to do this?
I really appreciate any help i can get - I'm going nuts trying to figure this out :)
Btw. i don't use Interface Builder - All UI elements are coded manually.
5 ways to do it here:
1) Let the Search View Controller be the delegate of action in the Search Type View Controller, so that when the user selects a search type, it will be informed. Use protocol for proper check at compile time if you want, and remember to use assign instead of retain for the delegate, to avoid circular reference.
2) Set the UINavigationController delegate to Search View Controller (or whatever class you want to control it), and listen to the event when the Search Type View Controller is popped out.
3) Implement a "refresh" function in viewWillAppear: as suggested above, but this is not recommended, because the implementation of viewWillAppear: sucks and not reliable at all. Maybe good for simple app, but when the structure of your app gets complicated, forget it.
4) Use NSNotificationCenter. Your Search View Controller will observe all changes to search criteria, and in each child view controller, when the user changes it, post a notification. This is more complicated, but much more powerful and flexible than all the methods above.
5) Similarly, you can use Core Data to store all search criteria in an object, and listen to changes in that object using KVO. This is a bit more advanced and may be overkill, but if you know KVO, it makes life much easier in Objective C, so probably worth taking a look anyway.
Btw: It's good to do all the UI by hand coding to understand the concepts at first, but try to move to Interface Builder whenever you can. It is a much recommended way to work (there are countless threads on this in Stackoverflow or on the web, with more elaborated discussions on why IB is better).

How can I get a method to call in a class while the user is in a view of another class?

I have an iPhone app based on a tabBar and tableViews. I want the user to be able to click on one tab and access options for filtering the data in the initial tableView.
The problem I'm having is that while the user is selecting filter criteria, I want the main table (not visible) to update. The reason this is important is that I want to show how many cells are still in the table as it is being filtered in the navigation bar.
Currently, the method for filtering the main table (-handleFilter) is called in the viewWillAppear method of my rootViewController class. How can I call this method from my "searchOptions" class?
Thanks for the help!
It sounds like you're conflating too much between your model and your controllers (assuming you're following the MVC design pattern). The other controllers besides the main table should be able to query the model themselves to display the count information without asking the main table controller.
I could be misunderstanding something though, a little more information on what data you're using and how it's being filtered in the controllers attached to the other tab bar items would help.
The most straightforward way would be to give the options controller a pointer to the list controller. Then you can call the method directly.
Other options include defining a method/property on some global object (like your app delegate) to access the list controller from elsewhere in the app, and using a more decentralized mechanism like NSNotificationCenter to pass that information around (rather an a method call), or relying on the model itself to notify all of the controllers accessing it when it changes (possibly using Key-Value Observing, or an explicit delegate protocol).