Data sent back to Uitableview - uitableview

I have a UITableviewController and I push another UIViewController in 'didSelectRow..' method.
I have user input controls (combobox, stepper) in this viewController, that when the UIViewController is popped , I would like to receive the newly entered data in the UITableviewController (and update the tableview accordingly).
I saw some questions/answers, and some said to use "Delegation/Protocol" approach, but did not find any specific example how to achieve this.
Can someone help?

Create a new file for your project and choose the Protocol file type. (We'll call it CallBackProtocol.) In the view controller that you push, create a property that has a type of id<CallBackProtocol> delegate;. Have your table controller adopt the protocol and, when it creates the view controller, set controller.delegate = self;.
Define a method in the protocol that lets you pass whatever data you need back to the caller. Implement that method in the table controller and call it from the second view controller just before popping it.
(Or use a NSNotification.)

Related

Calling a view controller class without popping up the view controller itself

I am upgrading my app version, hence needed to reduce two view controllers to one. Hence, I am calling the second view controller skipping the first one. But here I need to call the class of first view controller as there are some important declarations and implementations. How can I call first view controller's class without popping up its view controller.
This is confusing.
Why not taking all the initializing code from your first VC (copy-paste your properties, initialization in viewDidLoad etc.) to your second VC ?
Once you are done, get rid of the first VC as it is useless and make your second view controller the root.
A ViewController is meant to be "viewed", I suggest you don't just hide it, that's a really bad architecture.
If you don't need one view controller at all, then you can delete that View Controller from storyBoard, and also subclass your class as an NSObject class (lets call it DataProviderClass) instead of previously( UIViewController) subClass. It isn't a good idea to have a "dummy" view controller in a navigation Stack.
You can use your DataProviderClass class as a support file that can provide any data to your Second View Controller. And to perform calculations/methods in this class before launching your second VC, just run these methods in viewdidLoad method, by creating an instance of this NSObject class (DataProviderClass) and keeping a reference to it.
When you segue further, you can very easily even transfer the same reference of DataProviderClass.

How can I pass a data from UIViewController to a UITableViewController that is embedded in a container placed on that UIViewController?

I have an ios app written in swift and there I have a UIViewController with a container on half of the screen. This container has embedded UITableViewController and I want to display there some data that I'm fetching from my webservice. The problem is that to fetch the data from web service I need an unique id number that I store in the UIViewController. So how can I pass that data from one controller to another?
I have a project on github (written in Objective-C) that does exactly what you want. It has the horribly unoriginal name "test".
It has a master view controller with 2 child view controllers, each of which is a subclass of UITableViewController set up to manage static table views. (That detail doesn't really matter. It's the method of setting up connections between parent and child view controllers that addresses your question.)
The idea is that you set up your child view controller in a container view and link it in with an embed segue.
When you use an embed segue then prepareForSegue fires when the views are loaded. In your prepareForSegue you save a reference to your child view controller(s) if you need to call it/them, and make yourself each child's delegate if you need it to call you.
The code would be different in Swift but the concept is identical.

Send the value in Class of ViewController to Class of View

Is there any way that could send the value in Class of ViewController to Class of View?
Because I want to make a drawing board and I made a modal scene to set values about color, width .
I know how to send the value in modal scene to my ViewController , but now I need use those value in Class of View , or not Class of ViewController.
Sounds like you need to devise a protocol to open a delegation channel between the two classes. Then when the modal VC wants to send data to its delegate (its presenter, in this case), it can of its own volition.
The View Controller can have a reference to the View, therefore the View Controller can simply pass the values to View by calling a View's method or updating View's properties. This is a common pattern: View defines a Protocol and data/requests from View to ViewController go through this Protocol (the ViewController acts as delegate of View); and ViewController owns the View.
For inspiration on how to implement a farily decoupled design applying this pattern, you can have a look at documentation on UICollectionView and UICollectionViewController.
Other options, depending on what design you need, are Key-Value Observing or Notifications.

passing data to a childViewController from the view controller that holds the container view controller

In my application I created a container view(called ContainerViewController) on the top of an another view(called GameViewController,where I keep the game logic and api callbacks).I also have other views and an api class.My container view contains 3 views and I am trying to pass some objects to one of my container view(VC1). Since GameViewController is only connected to ContainerViewController(meaning there is no segue directly, so I can't use prepareforsegue and I can't use presentViewController, since they childViewControllers), I can't pass data.
I also tried just getting the values from my deserializer class(inside vc1). The problem with that was the VC1 gets called before.
my question is, how can I set values of VC1 from my callback inside the GameViewController?
This is what my storyboard looks like:
Thank you.
You can do it many ways:
1st way : You can pass the data using Notification.
2nd way : You can create a separate singleton data model class to store your game data. Then you can access the object of that class among your view controllers to set the properties in view controllers.

Passing a variable from modal tableview to parent view

I have a detailView which has a button on it to bring up a tableView as a modal view to help the user giving a selection of names. I'm trying to figure out a simple way of passing the selected name to a textField on the parent detailView.
I have been building this project using Storyboard, but I am totally stuck at this point.
There are several ways to approach this kind of problem:
shared object: The parent view controller provides some object to the child that contains the state that the child is expected to work with and possibly modify. In this case, it might be a dictionary that contains an array of names to display and the index of the selected person.
child's properties: Often, the child view controller itself is the shared object. The parent instantiates the child, sets some of the child's properties (e.g. people and selectedPerson), and presents it. When the child is finished, the parent gets the properties of the child that it cares about (e.g. selectedPerson) before releasing it.
delegation: Sometimes the child view controller will need to interact with the parent in order to do its job. This is a good time to use delegation: establish some protocol that the child knows about and the parent implements, add a delegate property to the child, and have the parent set the child's delegate property to itself before presenting it. This way, the child can talk to the parent without depending on the parent class.
For the case you describe, where you just want to convey some piece of data back to the parent when the child is done, the second strategy above is the simplest.
Note: DetailViewController below should be replaced with whatever the class is of your detail view controller.
1) If you don't already have a subclass of UITableViewController, create one. This will allow you to add a property to the table view controller. Change the class of your modally presented controller in your Storyboard to this new subclass.
2) Add a property to your table view controller subclass like this:
#property (weak, nonatomic) id mydelegate;
3) In prepareForSegue in your DetailViewController, set the mydelegate property of the destination view controller to self which is a pointer to your parent detailView.
4) In your DetailViewController, create a method to receive the selected name:
- (void)userDidSelectName:(NSString *)name;
5) In the tableView:didSelectRowAtIndexPath method of your tableViewController subclass, get the name from the tableViewCell and call the userDidSelectName method on the delegate:
[self.mydelegate userDidSelectName:selectedName];
6) The correct way to to this would be to define a protocol that has a method userDidSelectName and then make the delegate pointer in step 2 require a class that implements that protocol. The quicker and dirtier thing to do is to make the delegate pointer in step 2 of type DetailViewController * instead of id. This will allow you to call userDidSelectName without the compiler complaining.

Resources