I have controller and presenter and I want to pass directly pass value from controller to presenter, not in the view.
Is it possible to do that?
Related
I want to know what is best practice when triggering an event from a base model class, which will have a common outcome on all View Controllers. Say I have a shared, global variable, which can trigger an event based on different inputs. Wherever I am in my app, I wish to show a popup with the same information. With my knowledge of view controllers and swift, my only option is to implement the same code in each view controller, as I have to add that same popup to each view, depending on where I am.
Wouldn't it be nice if I could pop that popup from a global-root'ish view controller?
Is there a general coding-practice I've missed?
My first idea is to use a navigation controller (UINavigationController) as the root controller, and then just use
navigationController?.topViewController
from any view controller added to that navigation controller.
There might appear a problem: in case if you already displayed the alert view controller, the app will try to present an alert controller on top of another one. You can keep some kind of global state, or check the type of topViewController.
In order to respect the MVC pattern, I would like to dissociate the view from the controller.
For example
QuestionView (inherits from UIVIew)
QuestionViewController (inherits from UIViewController
In my controller, I set the view self.view = ...
But when I push a button in the view, it should call a method from the controller BUT the view shouldn't know its controller right ?
So how can I link the view to the controller ?
Set button target as QuestionViewController's object programmatically.
With iOS in most cases you can get the job done with only sub classing the controller part, and not the views. So you use the UIKit provided classes straight 'out of the box'.
It's is possible because:
The layout: this can be stored in a Nib file and be loaded by the controller.
responding to user events: the UIcontrols have generic callback mechanisms: delegates and actions. The 'connection' is either made in the nib file, or in the controller code.
Personally, I only subclass views when I need custom drawing.
So, the View INSTANCE obviously 'knows' its controller, but it is all done through generic interfaces, and so the view CODE is ignorant of your controller.
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
i have a view with label when button pressed moves to second view controller with a picker view , i need to retrive the selected value and then to pass to first view's label,
i have used delegates and protocols to acheive this,
but is there any possible ways like referencing like etc to do this?
You can use a singleton class to store the selected data and then can retrieve it wherever you want. Just like you get UIApplication's instance from 'sharedApplication' method, its the same object in the memory through out the application lifecycle.
I have a UIViewController that I want to display a second UIViewController as a dialog. Basically, the user presses a button in the first controller, then the second controller pops up and the user makes a selection.
When the user presses Save in the second controller, how does control get passed back to the parent controller, and how can I extract the user's choice from the second controller?
Consider looking into setting up a Modal View Controller under your root controller.
You could set up a property that stores the choice or selection. Its value would be accessible to the root controller through its instance of the modal view controller.
It seems like there are two leading ways to do this:
Create a custom delegate. Make the parent controller the delegate of the child controller. Have the child controller call an appropriate method in its delegate and simply use popNavigationController when you're done.
Create a member variable in the parent controller referring to the child controller. When you display the child controller, store it in the member variable. Then override viewWillAppear: in the parent controller so that if the child controller reference is non-nil, it will interrogate the child for whatever information it needs. Then set the reference back to nil.
I personally like the second better. YMMV.