Call view controller with xib from storyboard? - ios

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.

Related

Many to One Segue

I have a ProductDescription ViewController that gets called from a ProductTable UITableView that I have placed in many ViewControllers.
It doesn't seem very efficient to ctrl+drag a segue for each tableView in the Storyboard, as I have approx 20 of them.
How does one do this programmatically?
You have several options within UIKit to programmatically show a view controller without using a segue:
Push a view controller onto the navigation stack:
pushViewController:animated:
showViewController:sender:
Present a view controller modally:
presentViewController:animated:completion:
The real answer here is to use storyboard references. You shouldn't have the same thing in twenty different spots all trying to link to the same view controller to the point of asking this question.
So, let's create Product.storyboard, a storyboard which simply has two view controllers:
ProductTableViewController
ProductDescriptionViewController
And the appropriate segue between the two controllers.
Now, everywhere else in any of your other storyboards that want to use these controllers with this relationship, simply add a storyboard reference, add a container view controller, and add an embed segue between the container view and the appropriate view controller in the product storyboard.
You can accomplish this same effect even without using storyboard references. Ultimately, the main point is to use container views and make embed segues from everywhere you need this relationship to the first of these two controllers, and then there's just a single relationship created between the two product view controllers.

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.

Segue to a subclass of some view controller

I'm using a storyboard. Let's say I have a view controller that's named MYviewController.
In - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender; I would like to substitute the view controller that I'm segueing to, by one of its child, for example: MYviewControllerChild1 OR MYviewControllerChild2. The child that's segued to depends on the sender parameter.
These view controllers have the same scene (in the storyboard). Only their behaviour is slightly different.
I have a tableView that shows the user the settings of the application. When he clicks a cell, it segues to a viewController where he can modify the value of some setting. Some of theses are alphanumeric, others are numeric. Depending on which cell is clicked, I'd like the input viewController to format the value accordingly (if it's a decimal value I'll use a NSNumberFormatter for example).
Is that possible?
As mentioned in comments to your OP, I believe you should handle this kind of scenario in one viewcontroller.
However, if you insist on using separate controllers, maybe because you think the functionality will be expanded later down the line and therefore add more diversity, you need to handle this by creating multiple storyboard scenes - one for each child controller.
The destination view controller in prepareForSegue is imposed by the viewcontroller at the end of the segue in the storyboard. I don't think there is any way to override that.
As described, your problem isn't really a good candidate for a storyboard. If you use a storyboard you will have to create and sync multiple scenes. Several possible solutions::
Create multiple storyboard scenes and invoke them manually via performSegueWithIdentifier.
Use a nib file instead of a storyboard for this scene. You can use a single nib file since the view controller is created outside the storyboard with [[VCClass alloc] initWithNibFile: bundle: You can create the appropriate view controller class and pass the same nib file to all instances.
Use a single storyboard scene and view controller and pass in typing information in your prepareForSegue.

UIContainerView call parent method

I am a very new iOS dev and need your help.
I have a simple app, made from:
1 view controller that contains
3 UIContainerView, each one is linked to its own view controller and class
A view (the player)
Here is an image of my storyboard so it will be easier to understant:
=> I need 10 rep to post the image as an image
What I want is that when I click the play button on the cell inside "View Controller Search" it calls a method or function from the parent (main?) view controller including the url of the file to play.
I have already a working action on the play button, I have found how to get the url from it and printed it using NSLog so everything is fine from this part. My only problem is to find a way to communicate with the main view by sending the url.
If hope I am clear enough, thank you for your time.
You've got two options:
The quick and dirty one is to use the parentViewController property of your contained controller, cast it to the type of the parent view controller and call a method on it.
The right way is to define a delegate protocol and property for your search view controller, and make the parent view controller conform to it. Then, in the prepareForSegue: of the parent view controller, set the parent as the search controller's delegate.
prepareForSegue: will be called three times when your parent view controller is loaded, once for each embed segue that you have defined in the storyboard above. Just like when you push on a navigation controller, this is your opportunity to configure the destination view controller. You can give each embed segue in your storyboard an identifier to help with this process.

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

Resources