Swift Communicating between view controllers - swift

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.

Related

Swift - Reuse the old instance of view when switch between Views and Passing data over views

I am trying to create a simple app and here is how it works:
When the user clicks the button "Picking", it'll show a tableview. And when the user selects a row in the table view, it will show the first view and display the data on the label.
I have 2 problems:
Every time I click the button "Picking", a new instance of the table view controller gets created; but does not reuse the existing one. How can I make it reuse the old one?
I want to pass the selected data to the root view. I tried by creating a segue to show the root view when user selects a cell and then passing the data on prepareForSegue method. But, I faced the above problem of memory leak. How do I pass the data between screens without leaking memory?
It sounds like you may be making a circular view hierarchy. When you tap on the cell in the table view, if you're pushing a new instance of the first VC, that is the wrong way to do it IMO.
Instead, you should pass the data back to the first VC then dismiss the second VC. Then, when you tap on the "Picking" button again, it's a new instance of the table view but your previous instance has already been removed from memory.
This allows for the smallest amount of memory to be used at any given time. When the table view controller isn't on the screen, it doesn't need to be in memory.
IMO, the best way to pass data up a navigation stack is to use a protocol and delegate. It's similar to how Objective-C protocols and delegates worked if you're familiar with that. Here is Apple's documentation on both. The delegate section is about 1/3 the way down the page.
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

"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

Data based on previous viewController, Class or rootViewController

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.

share data between 2 different viewcontrollers sending data to the first that called the second :S

I have one viewController that calls one TableViewController... the user selects one element in the table and I want to close the tableviewcontroller and to share what the user selected with the first controller.
Which is the easiest way to do it?
Protocols and delegate are made for these purposes only i.e do something at some place when an event is occurred .. Rest it depends on you and your requirements.
Please check this answer
What's the best way to communicate between view controllers?
How do I share an object between UIViewControllers on iPhone?
If you are using storyboard
iOS storyboard passing data navigationViewController

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.