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];
Related
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!
I am using PaperFoldMenuController to create a cool SlideMenu in my App.
In the AppDelegate I define the viewControllers and add them to the SlideMenu Array:
// Init viewControllers
RootViewController *rootViewController = [[RootViewController alloc] init];
LivetickerViewController *livetickerViewController = [[LivetickerViewController alloc] init];
// Add view controllers to array
[viewControllers addObject:rootNavController];
[viewControllers addObject:livetickerNavController];
// Add to PaperFoldView
[_menuController setViewControllers:viewControllers];
I use a storyboard for designing most parts of the LivetickerViewController. But all changes are not shown. I checked that Storyboard is connected with the view.
What is the problem in the code ?
If you've designed your LivetickerViewController in the storyboard, then you need to get it from the storyboard not with alloc init.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
LivetickerViewController *livetickerViewController = [storyboard instantiateViewControllerWithIdentifier:#"livetickerViewController"];
Make sure that the name of the storyboard is what is actually in your project, and that you have given the view controller an identifier that matches what you pass in the instantiateViewControllerWithIdentifier: method.
If you made rootViewController in the storyboard, you should do the same thing with it.
Trying to "instantiate" my initial view controller from the app delegate. I trying to populate an NSMutableArray from the app delegate. A property of the view controller "myMutabelArray" gets an array that is created within the app delegate. With the code below the array is uneffected, even though it's count is 4 (has four objects), as created in the app delegate.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle: nil];
ViewController *controller = (ViewController*)[mainStoryboard instantiateInitialViewController];
controller.myMutableArray = mutableArrayCreatedInAppDelegate;
When I log the count from within the AppDelegate I get 4.
When I log the count from within the ViewController I get 0.
I also tried the following which makes me suspect that I am not getting a pointer to the view controller as needed.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle: nil];
ViewController *controller = (ViewController*)[mainStoryboard instantiateInitialViewController];
[controller.view setBackgroundColor:[UIColor lightGrayColor]];
Try the following:
ViewController *controller = (ViewController*)self.window.rootViewController;
It will return the initial view controller of the main storyboard.
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.
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.