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.
Related
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.
So i'm using a storyboard AND a xib.
I use a tableview in my ViewController and it has a custom header as a xib file (the VC is the File Owner).
I want to present my new VC when swiping on the header, I added a swipe gesture to the xib using IB, but now i'm having problem presenting it.
Crash comes when i'm trying to instantiateViewControllerWithIdentifier. I made a property of my presented VC. And set the right Identifier "swipeRight".
- (IBAction)swipedRight:(id)sender {
NSLog(#"right");
if (_mpvc == nil) { //user those if only so the use wont push it twice and more
_mpvc = [self.storyboard instantiateViewControllerWithIdentifier:#"swipeRight"];
}
[self presentViewController:_mpvc animated:YES completion:nil];
}
The error:
Cannot find executable for CFBundle 0x9022120 </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/AccessibilityBundles/GeoServices.axbundle> (not loaded)
If you are inside a XIB file, how do you know which storyboard are you using? So first you have to instantiate storyboard which contains that viewController with specified identifier:
UIStoryboard *st = [UIStoryboard storyboardWithName:#"StoryboardName" bundle:nil];
After you have to instantiate viewController with this storyboard that you created:
UIViewController _mpvc = [st instantiateViewControllerWithIdentifier:#"swipeRight"];
Then finally you have to present the viewController:
[self presentViewController:_mpvc animated:YES completion:nil];
Hope it solves your problem!
I am using storyboard in my app, :
I have Button in my View, On click of button i want to navigate to new View
But when i click on button nothing happens,
Here is my Code:
- (IBAction)JoinClicked:(id)sender{
JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:#"JoinWithViewController" bundle:nil];
[self.navigationController pushViewController:detail_view_controller animated:YES];
}
Where i am doing mistake , please help.
Thanks in advance.
Set storyboard id of view controller in story board in identity inspector
Make a reference to your storyboard like
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
then to your controller
YourController *vc = [storyboard instantiateViewControllerWithIdentifier:#"YourController"];// storyboardId
[self.navigationController pushViewController:vc animated:YES];
and you can also make segue and do
[self performSegueWithIdentifier:#"YourSegueIdentifier" sender:sender/nil];
use this one :
- (IBAction)JoinClicked:(id)sender{
[self performSegueWithIdentifier: #"JoinWithIdentifier" sender: self];
}
and add segue in storyborad like
Make an identifier for your push-segue in storyboard.
then use
[self performSegueWithIdentifier:#"YourSegueIdentifier" sender:nil];
When using story boards you should use segues for as much navigation as possible.
Your code seems fine, please verify your outlets correctly configured or not in xib or storyboard.
Edit:
You are using storyboard and wants the xib file to get pushed onto your view controller if I am not wrong, you can drag another view controller in storyboard and name it JoinWithViewController and push using segue on button click. That is much easier than what your trying to do here.
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.
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]