Call a view controller programatically using storyboard id iOS - ios

What I Want?
In MainViewController I open a view from a xib programmatically. This
xib view contains a UIButton. The xib opens successfully.
On click of that UIButton I want to move FeedBackViewController
My code
This is the code I am using to move FeedBackViewController on click of the UIButton
FeedBackViewController *view=[self.storyboad instantiateViewControllerWithIdentifier:#"FeedBackView"];
[self presentViewController:view animated:YES completion:nil];
But it is crashing and I am receiving the following message:
Terminating app due to uncaught exception 'NSInvalidARgumentException', reason: 'Application tried to present a nil modal view controller on target FutureOrderViewController: 0x199a4a30.

Check these things
Check the identifier of the viewcontroller, if it is the same that you mentioned in storyboard
Make sure that your view object is not nil. Set a breakpoint on that line and on the debug area at the bottom see whether the object is not nil. If it is nil then problem lies in the storyboard connection
If your current viewcontroller is not launched from storyboard then get storyboard object like this :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController * feedbackVC = [storyboard instantiateViewControllerWithIdentifier:#"FeedBackView"] ;
[self presentViewController:feedbackVC animated:YES completion:nil];

whenever you want to load a VC as the way you mentioned, you need two things.
Thing 1: Specific storyboard ID. you will find this by:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Your_Storyboard_Id" bundle:nil];
In my case this is Main
Thing 2:
You need your View Controller id. Just click on the View Controller you want to go from storyboard and give a suitableName there.
Then invoke this like:
UIViewController * yourVC = [storyboard
instantiateViewControllerWithIdentifier:#"YourVC_ID"] ;
And if you want to present it modally you already did this. :)
[self presentViewController:yourVC animated:YES completion:nil];

Try this
UIStoryboard *story = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
FeedBackViewController * feedbackVC = [story instantiateViewControllerWithIdentifier:#"FeedBackView"] ;
[self presentViewController:feedbackVC animated:YES completion:nil];
Where FeedBackView is your storyboard id of FeedBackViewController.

Related

Connect StoryBoard Button with Programmatically build UITabBarController

i build two project
Story Board -> Login Screen
Programmatically -> UITabBarController
both are different Projects but managed With MVC structure
now i want to connect StoryBoard Button with Programmatically build UITabBarController
Please Help me to solve this Problem.
To create a view controller:
UIViewController * vc = [[UIViewController alloc] init];
To call a view controller (must be called from within another viewcontroller):
[self presentViewController:vc animated:YES completion:nil];
For one, use nil rather than null.
Loading a view controller from the storyboard:
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self presentViewController:vc animated:YES completion:nil];
Identifier of your view controller is either equal to the class name of your view controller, or a Storyboard ID that you can assign in the identity inspector of your storyboard.

Programmatically load ViewController from Storyboard in app with multiple storyboards

I am working on an application that is transitioning to having multiple storyboards for different sections of the app. They are linked together by programmatically loading them at the appropriate times.
In a function for when a UIButton is tapped, it should programmatically load a storyboard. The code is like so:
- (void)loginTapped:(id)sender {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Home.storyboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"homeStoryboard"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
}
The storyboard is named the name listed in in the function:
and is listed as a target in the project settings appropriately:
and has the appropriate ID like so:
However, even after setting all this I get the error:
'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Home.storyboard' in bundle NSBundle
I have tried to follow the advice from this post here, but nothing has worked. How do I get this storyboard to load?
The solution (as mentioned in the comments above) is to not include the .storyboard when providing the name of the storyboard.
Old
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Home.storyboard" bundle:nil];
New
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Home" bundle:nil];
Try this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Home" bundle:nil];
To call any ViewController programmatically in ios using Objective c:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:Home bundle:nil];
yourDestinationViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#homeStoryboard];
[self presentViewController:vc animated:YES completed:^{}];
yourDestinationViewController in the code is your view controller.h file.
You want to import that viewcontroller.h file in your program, where you want to call the View controller programmatically.
and to create the property like below,
#import "viewcontroller.h"
#property viewController *yourDestinationViewController;

Present View Controller modally from xib file

I have a xib file associated to a UIViewController custom class (ZocaloPlayerViewController)
and im trying to present a UIViewController (made in storyboard) modally with flip horizontal
but i am getting an error:
This is how im trying to present the view controller
ShowDetailsArtistAlbumViewController *vc = [[ShowDetailsArtistAlbumViewController alloc] initWithNibName:#"ShowDetailsArtistAlbumViewController" bundle:nil];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:^{
}];
But i am getting this error...
Uncaught exception: Could not load NIB in bundle: 'NSBundle </var/mobile/Containers/Bundle/Application/57C2AD20-51DB-4C43-BB8F-2E8F22CD2E2A/MyMusicApp.app> (loaded)' with name 'ShowDetailsArtistAlbumViewController'
I was trying with
[self.parentViewController presentViewController:vc animated:YES completion:^{
}];
But nothing happens...
EDIT:
I tried this way
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ShowDetailsArtistAlbumViewController *vc = [sb instantiateViewControllerWithIdentifier:#"ShowDetailsArtistAlbumViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:^{
}];
The view controller is presented but im getting a black view but nothing inside
You say "I have a xib file associated" but the problem is that you don't. The runtime is looking for a xib file called ShowDetailsArtistAlbumViewController.xib but you don't have one. You need to rename your xib file to match the expectations of the runtime.
Alternatively, you may be creating your view controller incorrectly. You are saying this:
ShowDetailsArtistAlbumViewController *vc =
[[ShowDetailsArtistAlbumViewController alloc]
initWithNibName:#"ShowDetailsArtistAlbumViewController" bundle:nil];
That is why the runtime thinks you have a nib called ShowDetailsArtistAlbumViewController.xib - you are telling it that, right there in that line!
That's fine if there is in fact a nib called ShowDetailsArtistAlbumViewController.xib. Your problem is there is not. You have lied to the runtime and so naturally it crashes.
If you actually wanted to use the view controller in the storyboard, that's not how to access that instance. What you are doing is creating a different instance. If you want the instance in the storyboard, you need to talk to the storyboard and tell it to instantiateViewControllerWithIdentifier:.

iOS: Call segue from coded UIViewController outside of Storyboard

I have a custom UIVieController class that it's not included in any StoryBoard, I need to call from this class an UIViewController that is included in a StoryBoard, I tried many ways to do this but it doesn't work, always get a NSException because when I try to instance the UIViewController of the StoryBoard and get a nil object.
I don't know why it's happening this, I really appreciate any help with this.
Thank you.
Just to clarify in this code I omitted the pushing of the view to the UINavigationController.
I tried this.
+(void)loadSocialMediaAppSegue:(AXISAppDelegate*)delegate withName:(NSString*)segueId{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
AXISWebViewViewController *axisWebView = [storyboard instantiateViewControllerWithIdentifier:segueId];
}
and this
+(void)loadSocialMediaAppSegue:(AXISAppDelegate*)delegate withName:(NSString*)segueId{
AXISWebViewViewController *axisWebView = [delegate.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:segueId];
}
Ok the first thing you need to do is to add a identifier to you view controller at Storyboard, you can do that opening the Storyboard's Identity Inspector and fill the storyboard id field.
Now you are able to call this UIViewController from any place you want like this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"the name of your storyboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"the id of your view controller"];
I hope this can help you.
Please post the code you are using to instantiate the VC from the Storyboard.
In general:
To instantiate a View Controller from StoryBoard:
MyViewController *MyVC = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"MyViewController"];
Make sure you set the Storyboard ID and Restoration ID and Class name to "MyViewController" or whatever you want to call it and use that as the parameter for instantiateViewControllerWithIdentifier:
Then if you are in a navigation controller, simply push the instantiated view controller onto the navigation stack:
[self.navigationController pushViewController:MyVC animated:YES];
or present it modally:
[self presentViewController:MyVC animated:YES completion:nil];

Load UISplitViewController programmatically from another Storyboard

Currently I am using two iPad storyboards in my project.
The first storyboard has a Login screen and a tableview controller. I want to call the second storyboard from the first storyboard tableview controller, when the cell is clicked. Normally it's easy, but here the second story board has a UISplitViewController.
MainSVC *baseView = [[MainSVC alloc] init]; //UISplitViewController
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:#"Mail_iPad" bundle:nil]; //Second Storyboard
baseView =[storyBoard instantiateViewControllerWithIdentifier:#"MainSVC"]; //MainSVC = Storyboard Name
[self presentViewController:baseView animated:YES completion:nil];
This code is not working. I searched in google, but couldn't find the best solution.
How can I call second storyboard splitview controller programmatically?
Use this code. This should be work. !
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:#"Mail_iPad" bundle:nil];
UISplitViewController *split = [storyBoard instantiateViewControllerWithIdentifier:#"MainSVC"];
self.view.window.rootViewController = split;
Do not allocate another instance of split view controller. It should be done like this
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:#"Mail_iPad" bundle:nil]; //Second Storyboard
MainSVC *baseView =(MainSVC *)[storyBoard instantiateViewControllerWithIdentifier:#"MainSVC"]; //MainSVC = Storyboard Name
[self presentViewController:baseView animated:YES completion:nil];

Resources