How to get a existing controller object from storyboard file? - ios

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.

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.

Communicating between Views Instantiated By Storyboard

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.

Is it possible to use UIStoryBoard in a different UIStoryBoard's UIViewController?

I want to divide my flows in a separate storyboards.
At this point, i have created my main storyboard which is side menu.
What i want to do is, when a menu item is pressed i want to be loaded from another storyboard inside the main view controller view.
Is it possible, if yes,
How to achieve this goal?
To initiate a view controller from a storyboard named #"myStoryboard", and if you are using Restoration ID to create your controllers, you can do :
[[UIStoryboard storyboardWithName:#"myStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"ControllerID"];
(of course, #"ControllerID" refers to the ID you've set for your controller in the storyboard)
You can use a storyboard for any part of a program. Storyboards are not an all or nothing concept. You can have just one view controller in a storyboard, or a small network that just represents a subsection of your app.
To use just one view controller from a storyboard:
Add a new storyboard to your existing project.
Add a single view controller to the storyboard.
Assign an identifier to the view controller in the inspector.
Use the UIStoryboard class to load the storyboard resource
Use -[UIStoryboard instantiateViewControllerWithIdentifier:] to create a new instance of that view controller.
Install the view controller some place in your app by doing something like pushing it on to a navigation controller, or run it modally.
Author:Jon Hess

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.

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