passing data using delegates between viewcontrollers without any navigation controller connection - iphone

There are many examples of passing data between two view controllers, where one view controller navigates to another view controller.
But is it possible to pass data using custom protocols & delegates between view controllers that are not connected by navigation controller?
So, an example could be: Three view controllers namely are, A,B,C. A navigates to B, and Bnavigates to C. I know how to pass data between A & B using custom protocols & delegates. But can we pass data between C & A. Thus A can be C's delegate and thereby can receive data from C. Is this possible?
Any help would be appreciated.

One way is use delegates for backward passing of data
Refer simple-delegate-tutorial-for-ios link for passing data from C to A controller.
Check basic-delegate-example link.
Another way is by posting notification. for backward passing of data
Check Comunicate-Two-Views link.

You can use some singleton class and implement delegate protocol in it. So you will have opportunity to pass data between any view controllers.

For Send data from second view controller to First Controller use following github project:-
https://github.com/mauli787/CustomDelegateDataPassing

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.

How to determine the source view controller of a transition

I've created three different view controllers in Xcode's storyboard and connected them via a Navigation controller and appropriate segues.
Is there a method which can be invoked in the destination view controller to determine from which view controller the segue was performed?
So I'm looking for the opposite of the "prepareSegue" method. Maybe there is a method like "sourceViewController" or similar....
Thanks you so far!
You will probably not get direct name of that class but you can assume and compare the class names with the property or objects of that view controller you just created.
For Eg. going from A or C to B you can compare the properties/objects of A/C in B with method isMemberOfClass/isKindOfclass.
Hope this works :)

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

iOS Development: How can I return data from a modal view?

If I have a parent view controller that displays a modal view with a textfield to collect data from the user, what's the best way to return that data to the parent view controller? Currently, I assign the parent view controller as a delegate that's called from the modal view when the user enters the data. Is there a simpler/better way to return data from a modal view?
Thanks so much for your help!
Personally, I would have the modal view dispatch an NSNotification that passes the data. A delegate works too, of course. I think that both a singleton and a delegate mean tighter coupling, but I understand that some might disagree.
But I do use singletons, too, if I need access to data stored centrally from many different places in an app. I just wouldn't use it simply to pass data from a view to another.
When needing to store and pass data around I usually have a singleton class that I use throughout my app. This keeps things a little cleaner in separating my views from each other. Here's a simple implementation:
http://www.galloway.me.uk/tutorials/singleton-classes/

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).