Launch different ViewControllers on a button click in iOS - 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.

Related

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.

My viewController doesn't show up right

in my iPad-app I am trying to present one of my views with a modal formsheet-style.
Here's some code:
-(void)present
{
SecondViewController *modal = [[SecondViewController alloc]init];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentModalViewController:modal animated:YES];
}
I am using Storyboard, and I have put stuff like a textView and toolbars in the view I'd like to show. I have set the right class in Identity Inspector, and in the class-files I have checked that it's the right view appearing with putting NSLog(#"Right view");
When calling the void present, a view is appearing, but only as a dark-white square. Nothing og my content from Storyboard is in it, I even tried changing the background color of the view and the textView to see if something was just outside the square, but the whole thing stayed white. It feels like it's not using the view I created in storyboard, but I have set it to the correct class, and the NSLog gets printed out when calling it. I have not connected the two views in any way in Storyboard, the SecondViewController is just floating around, so that might be the problem? The button that calls for -(void)present is created programmatically, so I can't ctrl+drag it to the button either.
Why is it showing an empty version of my class?
In the "Identity Inspector" set a "Storyboard ID" for your ViewController, and then present it like this:
-(void)present
{
SecondViewController *modal = [self.storyboard instantiateViewControllerWithIdentifier:#"myStoryboardID"];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentModalViewController:modal animated:YES];
}
And if you're using iOS6, presentModalViewController:animated: is deprecated, so use this:
-(void)present
{
SecondViewController *modal = [self.storyboard instantiateViewControllerWithIdentifier:#"myStoryboardID"];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentViewController:modal animated:YES completion:nil];
}
Your problem is that you're assuming the program will intrinsically know where to find the, already laid out, view for this controller when that's simply not how storyboards work. The code you list about will create a view controller, but without an associated view it will simply show as a black square.
There's a few ways to solve your dilemma:
Add the modal transition as a segue in the view controller, this would be the simplest way and is what iOS storyboards expect you to do.
Move the view from the storyboard to an external .xib and call the initWithNibName:bundle: method to load this as your view controller's view. This is the best solution if you just want to programmatically load the view.
Load the view from your storyboard programmatically with the instantiateViewControllerWithIdentifier: method, this is probably a bad idea as it goes against the design of storyboards.
I can elaborate on those if you want.

Load view when button is clicked in Xcodes Storyboard

I just started to use Storyboard in Xcode. I have a starting view that has some buttons inside. Is there a way to load a special view when a button is clicked?
I only found this workaround:
-(IBAction)loadRegistration:(id)sender {
// load registration controller
UIStoryboard *storyboard = self.storyboard;
RegisterController *svc = [storyboard instantiateViewControllerWithIdentifier:#"RegisterController"];
[self presentViewController:svc animated:YES completion:nil];
}
You don't need any code to load a new view. You just control-drag from the button to the controller you want to present, and choose the segue type. If the controller with the button is in a navigation controller you can choose push or modal, if it's not, then you want to choose modal.
The way you did it seems ok. You can also connect the current View Controller to the one you want to present with a segue in the Interface Builder (ctrl + drag). When the button is tapped, you would call [self performSegueWithIdentifier:#"segue identifier"];. Of course, you set the id of the segue in the IB.
Even more simpler would be to connect the button with the new View Controller in IB (also ctrl+drag). This way you won't even need the IBAction.

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]

IOS Switch view and storyboard not works

I have a problem with switching views. I'm using a simulator with xcode 4.2
Storyboard contains:
NavigationController (initial view controller)
UIViewController which has relationship with the navigation controller
UIViewController (paired with my custom class: ViewEntryImageController) which hasn't got any relationship. Contains a button, a bottom toolbar with some toolbar button.
User come into the UIViewController, where he can see a ScrollView and in ScrollView some images.
Images has a gesture:
UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(openEntryImage)];
[image addGestureRecognizer:recognizer];
[recognizer release];
The openEntryImage function:
(IBAction)openEntryImage
{
ViewEntryImageController *controller=[[ViewEntryImageController alloc]initWithNibName:nil bundle:nil];
controller.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:controller animated:YES];
[controller release];
}
When I try to tap the image, the openEntryImage works as well (the effect is correct), but I don't see my ViewEntryImageController view and my buttons, I'm only see a black window.
I try to put a NSLog line into the ViewEntryImageController viewDidLoad function, and it works, so what is the black window and where is my view controller?
When I try to use pushViewController, on the new view I found a navigation toolbar with a back button, but no other controls.
I tried another version, I created a UIViewController class, but now with a xib file. I used it instead of ViewEntryImageController and it works. Why?
I want to use this controller in storyboard too.
The ViewEntryImageController class by itself has no information about how to build the dialog. But you can instantiate your view controller on your own from the storyboard:
UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:#"StoryboardFileName" bundle:nil];
ViewEntryImageController *controller = (ViewEntryImageController *)[myStoryboard instantiateViewControllerWithIdentifier:#"ViewEntryImage"];
This assumes a storyboard name of StoryboardFileName and that the view entry image controller has an identifier of ViewEntryImage set in the view properties (Attributes inspector, section "View Controller").
Try it like this :
ViewEntryImageController *controller=[[ViewEntryImageController alloc]initWithNibName:#"ViewEntryImageController" bundle:nil];
If you don't use .nib names but rather use storyboards, it's a bit harder. Create a segue from the controller to the ViewEntryImageController controller by holding ctrl and dragging from one view to the other. Click this segue and give it an identifier.
Then use the [self performSegue:#"identifier"]; function to present the next view.

Resources