So I found a example of how to transition from XIB to storyboard
NSString * storyboardName = #"CheckoutStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"Checkout"];
[self presentViewController:vc animated:YES completion:nil];
This works great, and then from there if I need to transfer data between storyboard scenes I can use this code to transfer data between segues with this on the first scene.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController respondsToSelector:#selector(setMyData:)]) {
[segue.destinationViewController performSelector:#selector(setMyData:)withObject:myObject];
}
}
and this on the second
#property (nonatomic, strong) NSString *myData;
However I cannot figure out how to transition from my XIB to my storyboard WITH data. I basically have a ton of variables that are on my XIB that I need on my storyboard, that then get passed around. I know how to do every step except passing data from the XIB to the storyboard. Any ideas?
I can't provide you any real code but you don't use segues with your above code.
In the example you provided you are creating an instance of the UIViewController you want to display, you are then presenting that view controller modally.
You simply access the variables directly from your desired view controller instance.
MyCustomUIViewControllerClass * vc = [storyboard instantiateViewControllerWithIdentifier:#"Checkout"];
vc.myData = <valueHere>
[self presentViewController:vc animated:YES completion:nil];
Related
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.
I have another ViewController inside the same story board to which I want to switch by clicking a button. The problem is when I try to control-Drag the second view controller to create an Outlet inside the ViewController.m file of the primary viewcontroller, an outlet isn't created.
In Android we can open a new Activity with different UI from inside of another activity. I am sure same can be achieved in iOS as well, so my question is how can I create an outlet of the second view Controller and open it programmatically?
It is not possible in iOS to create an IBOutlet from one ViewController to another ViewController, But you can use a UIStoryboardSegue for the purpose.
I will suggest you to follow Tutorial: Storyboards from Apple Documentation. It will help you understand how ViewControllers are actually connected.
Opening SecondViewController programmatically is possible by using Storyboard Identifier, You need to provide storyboard identifier name for the class you want to switch to, see this image , in my demo i have used secondViewController
Now use this code inside your buttonClick event method.
- (IBAction)buttonClicked:(id)sender {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
SecondViewController *secondViewController = (SecondViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"secondViewController"];
// Use this to show your UIViewController in modal transition.
[self presentViewController:secondViewController animated:YES completion:nil];
// Use this to show your UIViewController in push transition with navigation controller
//[self.navigationController pushViewController:secondViewController animated:YES];
}
Try this
Viewcontroller*homeVC;
[self.navigationController setNavigationBarHidden:YES];
UIStoryboard *storyBoard;
storyBoard=[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
homeVC=(HomeVC*)[storyBoard instantiateViewControllerWithIdentifier:#"HomeVC"];
[self.navigationController pushViewController:homeVC animated:YES];
As others mentioned, you cannot do this.
To override and launch VC and keep it's properties you can:
First, create segue between viewControllers: Zoom out the view by double clicking the background of interface builder. Ctrl+drag from one VC to another and choose type of segue (e.g. show).
Than create action for button, in which you can:
[self performSegueWithIdentifier:#"myIdentifier" sender:self];
After that, in -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender check identifier of segue, set new yourCustomClassViewController and voila!
if ([segue.identifier isEqualToString:#"myIdentifier"]) {
myCustomClassViewController *myCustomVC = [segue destinationViewController];
myCustomVC.oldProfile = newProfile;
}
I have two storyboards that contain one view controller each.
storyboard1 --------> viewcontrollerA
storyboard2 --------> viewcontrollerB
How do I pass a string from viewcontrollerA to viewcontrollerB?
Example:
viewcontrollerB.testString=#"The transfer of data worked!";
So far, I know how to transfer views, but I don't know how to transfer data.
This is my code to transfer views:
- (IBAction)moveToViewcontrollerB:(id)sender {
UIStoryboard *storyboard2 = [UIStoryboard storyboardWithName:#"storyboard2" bundle:nil];
UIViewController *viewcontrollerB = [storyboard2 instantiateInitialViewController];
viewcontrollerB.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:lessonOne_ViewController animated:YES completion:nil];
}
My question is, if I have a string in viewcontrollerB called "testString", how do I update that string in the viewcontrollerA IBaction method?
I only know how to transfer data using segues but since this viewcontroller is on another storyboard, a segue - prepareForSegue won't work.
Thank you in advance.
First of all set Storyboard ID to your SecondView Controller like below image.
Then add following code at your button click event.
- (IBAction)moveToViewcontrollerB:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard2"
bundle: nil];
SecondStoryboardVC * secondStoryboardVCOBJ_ = [storyboard instantiateViewControllerWithIdentifier:#"SecondClassStoryboardID"];
[secondStoryboardVCOBJ_ setStrValue:#"The transfer of data worked!"];
secondStoryboardVCOBJ_.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:secondStoryboardVCOBJ_ animated:YES completion:nil];
}
Storyboard2 is storyboard name.
SecondClassStoryboardID is Storyboard ID which you given in storyboard viewController like above image.
SecondStoryboardVC is the secondclass name which you want to present
I looked at many stackoverflow answers and tried most of them. May be I am doing something wrong but if I can get clarified, it will be wonderful.
My viewcontroller LoginViewController has a method which gets called and custom alertview which is a uiview is shown to user. After user enters 4 digit pin, I need to call another view controller from that custom alertview (uiview).
My current setup is:
LoginViewController.h:
#import "LoginPinAlertView.h"
#interface LoginViewController : UIViewController <LoginPinAlertViewDelegate,... > {
LoginPinAlertView *customAlertView;
}
LoginViewController.m:
-(void) viewDidLoad {
...
customAlertView.myLoginVC = self;
}
Inside my UI VIew (which a customer alert view)
LoginPinAlertView.h
#class LoginViewController
...
#property (nonatomic, retain) LoginViewController *myLoginVC;
LoginPinAlertView.m
....
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DestinationViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MyDestViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self.myLoginVC presentViewController:vc animated:NO completion:NULL];
}
...
But this is not working right now. What my requirement is, from UI View (which is not part of any contorller because this is custom alert view which many controllers are using), I wish to push/present another view controller.
hope this will help you ,
you cannot push/present view controller from a uiview class for it. you should check out this answer , https://stackoverflow.com/a/26011073/3767017
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];