Communicating between Views Instantiated By Storyboard - ios

I've got the following in my storyboard:
ViewA contains a Container and a UIView. The container has a Table View Controller embedded in it.
I want to action something in the Table which will affect the appearance of the UIView, so I need to call a method on ViewA from my Table View Controller.
I have followed this post - the 'Passing Data Back' section, which all makes sense.
Passing Data between View Controllers
The problem I have is the very last step where the controller is instantiated programatically.
The storyboard (or some ios framework component) is instantiating both Controllers for me.
So, my question is, how do I get a reference to the View instance that is automatically created for me
from the other Controller.
From ViewA how to I get a reference to my TableViewController in order to set the delegate?

The two controllers have a child-parent relationship. From the table view controller, you can access A, with self.parentViewController. From A, you can access the table view controller with self.childViewControllers[0]. You can also implement prepareForSegue in A, and access the table view controller as the segue.destinationViewController; you should set A as the delegate there.

At first you have to make storyboard identifier for your UITableViewController.
Than you can try this code...
ClassnameViewController *instance = (ClassnameViewController *) [self.storyboard instantiateViewControllerWithIdentifier:#"storyboardIdentifierSetByUser"];
instance.tableViewObject.delegate = self;
By using this you can access all the properties of ClassnameViewController.

Related

Objective C : change label with segue variable between two views with same controller

I have a main view controller that processes data. I want to send this data and display it in a modal view without using another controller.
I have something like this in my controller:
NSString *myData = #"something";
[self performSegueWithIdentifier:#"MySegue" sender:myData];
I have created a new view in the storyboard and a "Present modally" segue between them. I have created a label in the new view that I would like to change to display the content of myData.
But since there is no controller for this view I can't find a way to link the label to the data. Every advice I find (like PrepareForSegue) is for a two controllers configuration.
Every scene (at least every scene that you want to update custom controls on) should have its own view controller. If you create a scene without a custom view controller class specified, it will still instantiate a standard UIViewController object. Without a custom view controller, you have no way of updating the label on that destination scene.
The standard answer applies here. Give that destination scene its own view controller class, define a custom property in that destination class, have prepareForSegue in the source controller update that custom property in the destination controller, and have the destination view controller's viewDidLoad update the label on the basis of that custom property you set.

Call view controller with xib from storyboard?

For example, I want to create a button which will always push a simple view controller of the same class.
Of course I can place it in the same storyboard file with other view controllers. But it could be called from everywhere. So it means too many extra links in this storyboard.
Contrawise I didn't try to create another storyboard, not xib. But the view controller I need is so simple that the creating of a new storyboard for it looks like too extra.
You can invoke a view controller using an identifier instead of a unique identifier. Take a look a the method instantiateViewControllerWithIdentifier:
That enables you to create view controllers that "float" in your storyboard, without having segue links cluttering up the storyboard.
Once you've instantiated the view controller, you can then present it modally with presentViewController:animated:completion, push it onto a navigation controller, or whatever.
You can even instantiate a view controller with instantiateViewControllerWithIdentifier, then create your own segue object using initWithIdentifier:source:destination, and invoke that segue if you want to.

In ios 6, how do you use pass data between View controller holding 'Container View' object and Table View controller that is embedded in it?

I have a view controller with a label and textfield. I also added a container view which points to another table view controller with one section and 3 rows, basically static table view. I am unable to find any documentation / example which tells you how you pass data between View controller holding the Container View and Table View container embedded in the container view. I want both sided communication ?
The controller embedded in the container view (in storyboard), is automatically added as a childViewController of the controller in which the container view is added. Just to make sense of what I mean, add this line, in your viewDidLoad method of the base controller :
NSLog(#"children : %#", self.childViewControllers);
So lets say in VC1, you add a container view with an embedded controller VC2 (your tableViewController), then the above statement will log VC2 as a child of VC1. To access VC2 from VC1, you just use [self.childViewControllers objectAtIndex:0], and to access VC1 from VC2, just use self.parentViewController.
Hope this helps
If you set things up in a storyboard, you use segues. Just like most everything else in storyboards.
See Access Container View Controller from Parent iOS
There are new properties on UIViewController -childViewControllers and -parentViewController. You could use those.
Alternatively you could set up a relationship yourself. Be weary of retain cycles. Perhaps the parent owns the child and the child has a weak reference back to the parent.
[self.childViewControllers lastOject] or [self.childViewControllers objectAtIndex:index]; , depending upon how many child VC's you have.

How to get a existing controller object from storyboard file?

I'm new for storyboard file in iOS.
As I know, the objects (view controller object included) that I drag into the storyboard are existing instances, not a virtual class.
However, who owns those instances in application? AppDelegate? or others?
Now I'm trying to get a controller object from storyboard file and make the view of that controller shown with popover. (I don't want to drag the relation lines among the objects completely, and use -addSubView: to implement all the sub-objects in popover view.) I think there must be a way to access the independent controller in stroyboard file.
Any suggestions?
If I implement the view controller in MyViewController class and init a new object with [MyViewController new], this would be another object that's not I want, I think.
From within your view controller you can access the storyboard and instantiate the new view controller:
NewViewController *newVC = [self.storyboard instantiateViewControllerWithIdentifier:#"MyNewViewController"];
You may then use this in your popover.

How to subview a UITableViewController within another Controller using Storyboards

I have encapsulated all my table view logic on a UITableViewController that is linked to a view. This was done using storyboards.
I would like to embed this logic and view within another view controller / view (kind of like a header information with a scrollable table beneath.)
I have the following components:
CustomViewController which is linked to a UIView (dragged in from storyboard)
CustomTableViewController which is linked to a UITableView (dragged in from storyboard)
Essentially I am trying to mimic the scenario of the Stopwatch in the iOS clock app
What is the beast approach to this?
How is it done programatically?
Can this be done on the storyboard somehow?
Any help would be greatly appreciated. Thanks
Ok figured it out. This solution is iOS 5 specific since this feature was added there. This method works with storyboards.
Setup: The intention is to host one view controllers view and logic within another controller.
Since there is no intrinsic way to reference the child view controller in the storyboard, we need to give the view controller a name. This can be done by filling out the "Identifier" attribute on the controller in the storyboard. NOTE: Make sure you are giving the controller the identifier and not the controllers view.
Instantiate the controller you want to aggregate. This can be done from the hosting controller.
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"ControllerIdentifier"];
Add the child controller to the parent controller
[self addChildViewController: controller];
Add the child controllers view to the parent controllers view. Note if you have a place holder view in the parent controller you wish to add the child view to, then this is where you do it. Here I add it the a UIView called stage in the parent controller.
[self clearStage];
[self.stageView addSubview:controller.view];
presentedController.view.frame = self.stageView.bounds;
And that is it. Pretty simple. I have used it successfully with switching controllers and view in a home made tab control. The sub controller enlists its views in the view lifecycle, so the viewDidLoad, etc all work as expected in this child view controller.
Hopes this helps someone.

Resources