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];
Related
I got a source code for an existing app. throughout the app the developer didn't use storyboard for the user interface but rather he used interface builder xib files. As a new developer, i don't know how to develop iOS apps in xcode using xib files for viewcontrollers, i learnt the storyboard way. Now I want to add more screens to the apps source code, i created a new storyboard file to define my new screens, how do I navigate to the entry point of this new storyboard from a button action i defined in one of the xib files so that i can easily implement my features.
while searching i got some suggestions like this one
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"newStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"myViewCont"];
but i dont think i understand how to implement this even if this can be in a button IBAction method. and besides subsequent navigations would be segues through the view controllers i define in the new storyboard
It's right suggestion.
You should get storyboard object by its name. Then you should create your viewController using [storyboard instantiateViewControllerWithIdentifier:#"..."];
Then you may navigate to your new VC using appropriate navigation method. For example:
[self.navigationController pushViewController:vc animated:YES]
ViewController identifier you should set in Interface Builder under section "Identity Inspector" in field "Storyboard ID".
That code you should place in IBAction method. But better would be to extract it in separate method. Something like this:
- (IBAction)buttonPressed:(UIButton *)sender {
[self navigateToMyNewViewController];
}
- (void)navigateToMyNewViewController {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"myNewStoryboard" bundle:nil];
MyNewViewController *myNewVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MyNewViewController"];
[self.navigationController pushViewController:myNewVC animated:YES];
}
Navigation from MyNewViewController your can implement as you want. It may be segues.
MyNewViewController *myVC = [self.storyboard instantiateViewControllerWithIdentifier:#"YOUR_VC_NAME_HERE"];
This will instantiate your ViewController, from here you can present it like so
[self presentViewController:myVC animated:YES completion:nil]
The image shows where you set the storyboard name that you're referring to
Try This it's working for me ,
Use below line of code :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main"bundle:nil];
LoginViewController *loginVC = (LoginViewController*)[storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:loginVC animated:YES];
We need to set storyboard id as per mention in below,
you can navigate one storyboard to another storybord:
first you import your viewcontroller class to appdelegate.h
AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];
appDelegateTemp.window.rootViewController = [[UIStoryboard storyboardWithName:#"storyboard_name" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
Like the way you use storyboard, use
MainViewController *mainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil]
[self.navigationController pushViewController:mainVC animated:YES];
The code about present another independent storyboard as follows:
- (IBAction)action:(id)sender {
UIStoryboard *secondStoryboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController *firstVC = [secondStoryboard instantiateViewControllerWithIdentifier:#"22"];
[self.navigationController presentViewController:firstVC animated:YES completion:nil];
}
If my storyboard like follow contain a UITabBarController the Pesentbutton with function -(IBAction)action:(id)sender; can't present another independent storyboard.
If my storyboard like follow contain a UINavigationController the Pesentbutton with function -(IBAction)action:(id)sender; presents another independent storyboard as expected.
Who can tell me the reason?More thanks!
Because in the first case self.navigationController is nil... Since no navigation controller is present. Try presenting from self or self.tabBarController
I have a combination of a storyboard and NIB viewcontrollers how can I segue from one to the other
This is the Scenario:
UITabBarController(storyboard)|- UINavigationController (subclassed in storyboard) -> [UIViewController (NIB)] -> UIViewController (in Storyboard)
You'll have to give the UIViewController in the Storyboard a 'Storyboard ID' (in the Identity inspector tab). Then instantiate that ViewController in your NIB ViewController's code like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"YourViewControllerId"];
[self.navigationController pushViewController:vc animated:YES];
In your UINavigationController subclass you need to instantiate your view controller with its view in a NIB(let's call it NIBViewController) and set that view controller as the rootViewController with the method setViewControllers:animated:. Do it in awakeFromNib like this:
- (void)awakeFromNib
{
[super awakeFromNib];
NIBViewController *viewController = [[NIBViewController] init];// It will look for the appropriate XIB as long as you did not change its name and it's in the main bundle, otherwise use initWithNibName:bundle:
[self setViewControllers:#[viewController] animated:NO];
}
Then, to go from the NIBViewController to other view controller contained in the storyboard just set an identifier in the storyboard for that view controller, instantiate it and push it or present it, as you want.
It is important to mark that in your NIBViewController instance self.storyboard will be nil because that view controller is not contained in a storyboard, so you need to instantiate a new storyboard(solution A**) or get the storyboard from the navigation controller(solution B):
// Solution A
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
// Solution B
UIStoryboard *storyboard = [self.navigationController storyboard];
UIViewController *storyboardViewController = [storyboard instantiateViewControllerWithIdentifier:#"viewControllerId"];
[self.navigationController pushViewController:storyboardViewController animated:YES];
** Please note that this is creating a new instance of the storyboard, so better not to try tricky things and avoid segues to previous view controllers contained in the storyboard or you will find that those view controllers are not the same ones... It's the first solution that came to my mind, but definitely you should go for the solution B.
How can I initiate segue from a custom UIView or custom UIViewController that is not on Storyboard? - they are created programmatically inside a parent UIViewController.
Although the destination UIViewController is on the storyboard.
Then just do it the old fashioned way. Instantiate it and present it modally or just push it if you are in a navigation controller. Hope this helps.
EDIT:
You can talk about segue only if it is in the storyboard. If Your source View Controller is not in it, you just present the next one as I said. You can instantiate your destination view controller from the storyboard:
MyViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self presentViewController:controller animated:YES];
From this point you will be "back in the storyboard" so you can perform segues in the destination View Controller.
First give storyboard id. Select the view controller than write a identifier(Storyboard ID)
Secondly, just open your .m file then write belove codes.
UIStoryboard * st = [UIStoryboard storyboardWithName:#"nameOfStoryboard" bundle:nil];
MyViewController *viewController = [st instantiateViewControllerWithIdentifier:#"YourStoryboardID"];
[self.navigationController pushViewController:viewController animated:YES];
Last line of code. Might also be
[self presentModalViewController:viewController animated:YES];
There is probably a simple solution but I can't figure it out.
I am using storyboards for the interface.
I start with a tab bar controller, but before the user is allowed to use the app the user has to authenticate himself trough a loginview which is modally pushed at the start.
I want to configure the loginview at the same storyboard, but I can't seam to figure out how to link the view controller at the storyboard and my code.
What I have done:
Create a new UIViewController subclass trough file > new > new file.
Drag a new UIViewController in the story board
Set the class in the custom class tab
drags a UILabel for test purpose.
run
No label...
Pull on a new UIViewController that will act as the login view controller onto the MainStoryboard. In the attribute inspector change the identifier to LoginViewController (or something appropriate). Then add
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
}
to the First view controller and the login screen will be loaded from your storyboard and presented.
Hope this helps.
The answer by Scott Sherwood above is most correct answer I found after lot of searching. Though very slight change as per new SDK (6.1), presentModalViewController shows deprecated.
Here is very small change to above answer.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
HomeViewController * hvc = [sb instantiateViewControllerWithIdentifier:#"LoginView"];
[hvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:hvc animated:YES completion:nil];
I'm new in this field. But if the first view controller is a navigation view controller and its rootviewcontroller is a table view controller. If you want to push a view controller like the LoginViewController when you click the cell, and you also want to go back to the table view by using the navigation bar. I recommend this way:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *controller = [sb instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
In this way, you can have the navigation.
By the way, I don't know why this kind of problem you asked will appear. I guess when the loginviewcontroller is created in the code, its view is not the view in the storyboard. If someone know the cause, please tell me! thanks!