For Example, I have 2 Storybards. In the first Storyboard I have a View with a button and when you press the button the second storyboard should appear.
How can I do that?
Take a look at the docs for the storyboard class
You can create a storyboard using
UIStoryBoard *storyboard = [UIStoryboard storyboardWithName:#"secondStoryboard" bundle:nil];
and get view controllers from it like
UIViewController *initialViewController = [storyboard instantiateInitialViewController];
or
UIViewController *otherViewcontroller = [storyboard instantiateViewControllerWithIdentifier:#"otherController"];
Once you've got your view controllers you can just push them onto your navigation controller I guess.
However, I don't know what will happen with using two storyboard objects in the same view hierachy - it's probably fine but you never know :)
Even though your question is a bit confusing, I think I know what you are trying to do.
You want to use two storyboards. UIStoryboard has a method to retrieve an instance of any storyboard with a given name. So first, set a name for your storyboards and view controllers in Xcode and then load them up, and then from within any view controller:
UIStoryboard *anotherStoryboard = [UIStoryBoard storyboardWithName:#"SomeStoryboardName" bundle:nil];
Afterwards, instantiate the desired UIViewController from any storyboard:
UIViewController *anotherViewController = [anotherStoryboard instantiateViewControllerWithIdentifier:#"SomeViewControllerName"];
You could then push it into your navigation stack, for instance:
[self.navigationController pushViewController:anotherViewController animated:YES];
Modifided to be generic here's the way I do this in my app...
UIStoryboard *alternateStoryboard;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate_iPad" bundle:nil];
} else {
alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate_iPhone" bundle:nil];
}
AlternateController *altController = [alternateStoryboard instantiateInitialViewController];
[altController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:altController animated:YES];
If your app is only iPhone or iPad that can be reduced to...
UIStoryboard *alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate" bundle:nil];
AlternateController *altController = [alternateStoryboard instantiateInitialViewController];
[altController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:altController animated:YES];
You'll probably want to change the last 2 lines to suit the presentation style that you want.
Related
We have old project we are trying to convert to SwiftUI but right now depending on the state of couple of things we are loading different StoryBoards. But in our case we just one to replace on storyboard at the time with SwiftUI. Any of you knows how can we accomplish this?
Here is an example of how we are loading each StoryBoard:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
Aceptado *targetViewController = (Aceptado*)[mainStoryboard instantiateViewControllerWithIdentifier:#"servicio_aceptado"];
UINavigationController *navVc=(UINavigationController *) self.window.rootViewController;
[navVc pushViewController: targetViewController animated:YES];
My question: How can I switch between viewcontrollers with 1 rootNavigationController in code, while maintaining customization I set in Storyboard and data that I load up in my viewcontrollers?
Currently I am implementing REMenu, which provides a simple dropdown tableview to change views with. When one of those cells is pressed in the dropdown, I want to switch my view. For example, if I press "Home", I want to go to my MasterViewController The method to switch views is called from within rootnavigationcontroller.m, and looks like the following:
REMenuItem *homeItem = [[REMenuItem alloc] initWithTitle:#"Home"
subtitle:#"Return to Home Screen"
image:[UIImage imageNamed:#"Icon_Home"]
highlightedImage:nil
action:^(REMenuItem *item) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController *vc = [sb instantiateViewControllerWithIdentifier:#"myNewTableView"];
// vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:vc animated:NO completion:nil];
}];
Clearly, however, something is wrong with this implementation, because it ignores the UI Customization I made in Storyboard, as well as a datacontroller I invoke in MasterViewController.m Any help on this matter would be greatly appreciated!
First, I am using storyboards for my navigation. I have come to a point where I am at a menu screen that goes off to 4 different views (just with a navigation controller) for 2 of those views I want it to check if the user has logged in and if not I want the LoginViewController/View brought up. I am very new to objective c/xcode and after searching for a solution this is what i have come up with. I do not know how to tie this in to my program. Does this just need to be linked to my button or am i completely off with what i am doing now?
if (self.appDelegate.userHasActiveLogin) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];}
else {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
}
What you could do is to create manual segues in your storyboard.
Ctrl-drag from your menu controller (orange symbol) to the child controller, select push. Click on the segue and give it an identifier in the attributes inspector. Now you can check if user is logged in and then conditionally call [self performSegueWithIdentifier:#"logged in segue" sender:self]; in your menu's VC.
The condition is that all the VCs must be in the same storyboard, but I suspect that is the case.
I'm trying to switch views in an iPhone application, but whenever I click on the button to bring up the second view, all I get is a blank screen, even though I've filled the view with buttons and whatnot.
I've checked the second ViewController in my storyboard file and its custom class is the one that it needs to be (When I originally created the second ViewController class a new interface file came with it, which I promptly deleted). What might I be doing wrong?
- (IBAction)Transition_NEXT:(id)sender
{
nextViewController = [[NextViewController alloc]
initWithNibName:#"NextViewController"
bundle:[NSBundle mainBundle]];
[self presentModalViewController:nextViewController animated:NO];
}
I don't think you're using Storyboards correctly. You shouldn't be using initWithNibName, as that's the old method of instantiating a view controller. Try using the new instantiateViewControllerWithIdentifier like so:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *nextView = [storyboard instantiateViewControllerWithIdentifier:#"NextViewController"];
[self.view presentModalViewController:nextView animated:YES];
If you already have a reference to your storyboard in your controller, then you shouldn't initialize a new one, but instead you should just use the reference you have.
UIViewController *nextView = [self.storyboard instantiateViewControllerWithIdentifier:#"NextViewController"];
[self.view presentModalViewController:nextView animated:YES];
I have 2 storyboard files in my app and I'd like to transition between a ViewController in one to a ViewController in the other. I've hooked up an IBAction in response to a button press on the first ViewController, which calls a method in the AppDelegate. I have verified that this signal reaches the AppDelegate method.
Here is the relevant method I have in the AppDelegate, however, no transition occurs. Can anyone tell me why, or is it a silly idea to have 2 storyboards?
-(void) presentSecondViewController {
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController* mainViewController = [mainStoryboard instantiateViewControllerWithIdentifier:#"main_viewcontroller"];
UIStoryboard* secondStoryboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController* secondViewController = [secondStoryboard instantiateViewControllerWithIdentifier:#"second_viewcontroller"];
[mainViewController presentViewController: secondViewController animated:YES completion: NULL];
}
You create a second instance of the initial view controller of the first storyboard. That instance was never shown on the screen as it is different from the one being already shown and thus probably won't show your second view controller. You need the instance of the view controller already being shown. The best way would be to change your implementation to
-(void) presentSecondViewControllerFromViewController:(UIViewController *)sourceController
{
UIStoryboard* secondStoryboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController* secondViewController = [secondStoryboard instantiateViewControllerWithIdentifier:#"second_viewcontroller"];
[sourceController presentViewController: secondViewController animated:YES completion: NULL];
}
and call it by passing the view controller that contains the button.