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.
Related
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.
I added one ViewController to my project and created one class.
After I bind this class to my ViewController.
In another controller I have method:
- (IBAction)login:(id)sender {
// How here I can do redirect to controllerViewController
}
There are two ways to push view controllers in the application.
1) by Segue
2) By View controller identifier
1) By Segue :
[self performSegueWithIdentifier:"SegueIdentifier" sender:self];
2) By View controller identifier :
Yourclassname *gotoYourClass = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerIdentifier"];
[self.navigationController pushViewController:gotoYourClass animated:YES];
- (IBAction)login:(id)sender {
ViewController *vc = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil]; }
In the storyboard give your view controller an identifier (under the Attributes Inspector) then use the following code, to bring that view forward.
IF YOU WANT TO USE PUSH THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self.navigationController pushViewController:vc animeted:YES];
IF YOU WANT TO PRESENT THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self presentModalViewController:vc animated:YES];
Note: In UIViewController please enter your view controller name which you want to push into another view.
If you want to comeback to the current ViewController later then use Navigation ViewController or else just use the presentedViewController of self(your current viewController)- no going back business, like they have previously illustrated.
For a simple block of execution(demo or practice) it's all the same but for a project application it completely matters to make the correct choice.
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];
I made a view controller A in storyboard and linked with other view controller. Now I have another view controller B and I want to write a method that can transit from B to A. How can I do this?
There is a segue to controller A in storyboard.
And controller B is loaded from another view I created programmatically so I can't set a segue in storyboard.
Since you are using storyboard. This is more accurate, try this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"YOUR VIEW CONTROLLER"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
But be sure to name your view's identifier otherwise it will crash and wont work.
Then when you want to dimiss the view:
[self dismissModalViewControllerAnimated:YES];
The new way of presenting a viewcontroller using the StoryBoard.
UIStoryboard* secondStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController* secondViewController = [secondStoryboard instantiateViewControllerWithIdentifier:#"Connect"];
[self presentViewController: secondViewController animated:YES completion: NULL];
The old way of presenting the controller Connect is like this
Connect *connect = [[[Connect alloc] initWithNibName:#"Connect" bundle:nil] autorelease];
[self presentViewController:connect animated:YES completion:NULL];
NSString *userid;
userid=#"123";
[connect setID:userid];
I want to call the setID function of the connect controller in the Storyboard way, how can I do that? Seems like I don't get an instance of Connect controller directly.
You should subclass the view controllers so that you can control what happens when users interact with it (unless your app can function on segues alone.)
In Xcode, File -> New -> File -> Cocoa Touch Class. Make a class like MyAwesomeViewController that subclasses (in your case) UINavigationController.
I like to make a custom method called NewVC in my custom view controller classes. It can do everything you list above, plus any custom setup:
+(MyAwesomeViewController *)NewVC {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: #"MyStoryboard" bundle: nil];
return [storyboard instantiateViewControllerWithIdentifier: #"MyAwesomeViewController"];
}
This way when you want to create a new one you can just call [MyAwesomeViewController NewVC] and it'll return a new view controller instance.
Why are you casting it as UINavigationController? Just do what you were doing.
Connect* connect = [secondStoryboard instantiateViewControllerWithIdentifier:#"Connect"];