Perform Segue from Xib programmatically - ios

I've search a lot but I can't do yet.
I've my .xib UI with some buttons, separated from main storyboard.
I need to perform an action when I press one of this button and show another view.
How can I do this directly from code such ad inside an IBAction?

You can't perform segues from XIB to Storybaord,
Instead of it you need
1) Get Storyboard's instance.
2) Instantiate a ViewController that is inside the Storyboard (By using Storyboard ID).
3) Push that ViewController to to your navigation stack.
Here is sample code
UIViewController *vc = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"yourVcIdentifier"];
[self.navigationController pushViewController:vc animated:YES];
#"MainStoryboard" - is the file name of storybaord (note. it's without .storyboard extension)
#"yourVcIdentifier" - is ViewController's Storybaord ID.
To give a Storybaord ID to some ViewController, open the storybaord, click on the ViewController and specify the "Storyboard ID" field.

Related

storyboard elements are not visible when using manual push navigation

i am using manual navigation on button click from one viewcontroller to another
nextpage *np = [[nextpage alloc]init];
[self.navigationController pushviewController:np animated:YES];
I included nextpage class on the viewcontroller in storyboard. But in this case the elements added via drag and drop are not visible while if i add manual elements via code they are visible from nextpage on navigating. Please help !
and If I add navigation from a button through a storyboard, in that case both coded elements and storyboard elements are visible. !
Try this.
Pass a identifier to your view controller in storyboard.
nextpage *np = [self.storyboard instantiateViewControllerWithIdentifier:#"your identifier"];
[self.navigationController pushviewController:np animated:YES];
Content you've added in the storyboard will only appear if you load that view controller from the storyboard. By simply alloc/initing a new view controller you're losing all of the information you've added in the storyboard.
You can create a segue from a view controller to another one (i.e. not from a specific button) if you want to manually push, just use performSegueWithIdentifier, or you can add an identifier to the view controller and use instantiateViewControllerWithIdentifier to create your VC from the storyboard.
first of all give storyboard id to viewcontroller then add following code
Nextpage *nextPage = [self.storyboard instantiateViewControllerWithIdentifier:#"NextpageIdentifier"];
[self.navigationController pushviewController:nextPage animated:YES];

Segue to UIViewController from a xib

I have a custom UIView which is a xib file and then a class that controls this view. I also have a storyboard which a lot of different view controllers inside. How do I perform a segue from the UIView to a specific UIViewController that is inside the storyboard?
Thanks
You can't do that. What you can do is give the UIViewController a storyboard ID, using the menu on the right of the interface builder.
Then call it programatically, like so:
MyCustomViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self.navigationController pushViewController:vc animated:YES];
You could put a ViewController into the Storyboard, set the class to your custom ViewController. Then, if you have class files for the view in the xib, set your custom view as the VC's view (already saves some code in loadView) and then just add a segue from the custom VC to the other view controller. To trigger that segue, you have to call [self performSegueWithIdentifier:#"identifierOfSegue"] in your custom ViewController.

How to connect storyboard to code

I created empty IOS project and want to create UI. I searched for information about connecting storyboard to code but there is nothing about it, just about xib files. So is it possible to use storyboard instead xib in empty project? And how?
You can get the storyboard object like this assuming you have a Storyboard file already in the Project
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:[NSBundle mainBundle]];
YourViewController *yourVC = [storyBoard instantiateViewControllerWithIdentifier:#"identifier"];
//Do whatever with the viewcontroller object
[self.navigationController pushViewController:yourVC animated:YES ];
Note:
Check the Storyboard name
Check your viewcontroller identifier in the Storyboard
First create an empty project with some class name
then create an storyboard from file->userInterface->storyboard
then give a name for the storyboard after that press storyboard in the left menu and place a viewcontroller and hold viewcontroller give class name as already created Uiviewcontrollerclass
finally press project choose main interface as ViewController.storyboard
if you wanna code suppose segue use [self performSegueWithIdentifier: #"nameofcontroller" sender: self];
if don want segue go like this
[self performSegueWithIdentifier: #"TheSequeName" sender: self];
The #"TheSequeName" is defined when you ctrl drag a button to open a new view controller on the storyboard
If you don't use segue, there are many variants to open a new view controller. The basic one is using a separate NIB (without storyboard)
SecondViewController *view = [[SecondViewController allow] initWithNibName:#"NibName" bundle:nil];
If you declare your view controller on Storyboard, you can use
viewController *view = [self.storyboard instantiateViewControllerWithIdentifier:#"viewStoryboardId"];
Then you show your viewController using navigation controller
[self.navigationController pushViewController:view animated:YES];
Hope it works
From the 'File' menu, create a new Storyboard.
Call it Main.storyboard.
In your project settings, set it as the main interface file under the Deployment Info.
New File - > Objective-C Class (of type View Controller) -> without Xib file
now on on storyboard, go to that particular xib file
in property window -> Show Identity Inspector -> change class to above added ViewController.
That's it, now you can control your storyboard xib files from code.

Launch different ViewControllers on a button click in iOS

My project is in iOS6.I have three UIViewControllers on my iPhone storyboard. On the first UIVC i have a checkbox and a continue button. If checkbox is checked and button is clicked then I have to launch second UIViewController.If checkbox is unchecked and button is clicked launch the third UiViewController. How do i do it? I have done this so far:
- (IBAction)btnContinue:(UIButton *)sender
{
if (self.shipToDifferentAddress)//Checkbox CLICKED->YES
{
//Launch ShippingVC
ITMShippingAddressVC *shippingVC = [[ITMShippingAddressVC alloc]init];
[[self navigationController]pushViewController:shippingVC animated:YES];
}
else //Checkbox unchecked ->NO
{
//Launch OrderedItemsVC
ITMOrderedItemsVC *orderedVC = [[ITMOrderedItemsVC alloc]init];
[[self navigationController]pushViewController:orderedVC animated:YES];
}
}
The self.shipToDifferentAddress is a bool variable telling launch this or that. I logged both the viewDidLoads and i got the output from both of them.There is some content on both the second and third UIViewControllers like buttons, labels etc. But they appear black.No content. I have back buttons on both of them and I can traverse back to first UIVC from either one of the one. SO what am i missing? I used segues incases where I had to go to a specific UIVC. Now i have a choice either second or third. How should i do it?Please let me know if you need more information. Thanks.
To present your ViewControllers that are in your storyboard, follow this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle: nil];
ITMShippingAddressVC *viewController = (ITMShippingAddressVC*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"ITMShippingAddressVC"];
Set the Identifier in your storyboard.. click on your view, and in the right properties menu set the Storyboard ID
Than you can push like normal:
[[self navigationController]pushViewController:viewController animated:YES];
While this will work for you-- when using Storyboards, ideally you want to use Segues. It requires much less code. Here is a good tutorial on using segues to push/present view controllers and pass data between them.
You have created your VC's, but they are not linked to the storyboard ITMOrderedItemsVC and ITMShippingAddressVC. So the interface that you added in storyboard doesn't get loaded.
What you could do is create a separate xib file for each of your VCs (New > file... > User Interface > View). Name the xib file the same as your viewController:
ITMOrderedItemsVC.xib
ITMShippingAddressVC.xib
Design your interfaces in these xib files (they are just like single-entity storyboards).
The xibs will be automatically loaded when you init an instance of the respective viewController.
This will work with your current code.

xcode 4.2 storyboard identifier

I'm writing an app in which I add some buttons to my view (mainView) by code. When the user taps on this buttons I want a new view (resultView) to show.
I have both of this views in my storyboard, but since there is no button on my mainView that is in storyboard I can't link the mainView and resultView to each other using storyboard. Everything must be done by code.
I want to know how I can link my two views by code and define an identifier for that link.
Thanks,
To switch the view controller programmatically, you have to set a identifier in the attribute inspector at section "View Controller". After that, you can just use the value inside your code. Here i used the identifier "Menu".
UIStoryboard *storyboard = self.storyboard;
MenuTableViewController *mtvc = [storyboard instantiateViewControllerWithIdentifier:#"Menu"];
[self presentViewController: mtvc animated:YES completion:nil];
There is also a description in the documentation, just look for
Instantiating a Storyboard’s View Controller Programmatically
Give the segue between the two an identifier in the Attribute Inspector in IB, then on your button press event call [self performSegueWithIdentifier:#"MySegueIdentifier" sender:theButton]

Resources