Helo all,
I am moving from one view controller to another using below code.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
[self.navigationController pushViewController:vc animated:YES];
Now i want to pass some data from one view controller to another like some variable.I have got this code to do this task but i am not making instance of another view controller.I am starting another view controller using storyboard.
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.str1 = str;
[self.navigationController pushViewController:secondViewController animated:YES];
Please tell me how can i pass data between view controllers in my way?
Solution does not work
decalre NSString in .h file.
#property (nonatomic, strong) NSString *UserId;
Synthesize in .m file
#synthesize UserId;
Code for navigation
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
vc.UserId=#"abc";
[self.navigationController pushViewController:vc animated:YES];
But vc.UserId does not recognized.
You can pass data between two ViewControler with the help of properties.
First in RoloBusinessCard.h ViewControler make a property like this
#property (nonatomic, strong) NSString *str1;
in .m file of this class synthesize it like this
#synthesize str2;
Now like this you can pass value
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
vc.str2=str2
[self.navigationController pushViewController:vc animated:YES];
If you are not able to get str2 in RoloBusinessCard
First Replace UIViewController with Your Viewcontroller Name like this
RoloBusinessCard *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
or
typecase UIViewController like this
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
(RoloBusinessCard *)vc1=vc
vc1.str2=str2
[self.navigationController pushViewController:vc1 animated:YES];
Hope it help.
Try Like this, Hope it will work.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
vc.str1 = str;
[self.navigationController pushViewController:vc animated:YES];
Here we are just using Navigation stack for passing data to the next controller
create a property in 2nd view controller
#property (nonatomic, strong) NSString *yourString;
in firstView controller action method
object of view controller which you will create on push method do
-(void)btnAtion{
object.yourstring = txtyourfirstvcObject.text
}
In swift, we can do the same
let's suppose we want to pass a Bool Value to the next controller so we can do :
Class ToViewController : UIViewController {
public var isSelected : Bool?
}
Class FromViewController : UIViewController {
#IBAction func onClickFlyingFrom(_ sender: UIButton) {
let tvc = self.storyboard?.instantiateViewController(withIdentifier: "ToViewController") as! ToViewController
tvc.isSelected = true
self.navigationController?.present(vc, animated: true, completion: nil)
}
}
Related
I have the following viewControllers:
When I click the "Go to gray" it loads the gray viewController using SWRevealViewController.
Here is my code:
#import "SWRevealViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)gotoGray:(id)sender
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SWRevealViewController *swrController = [storyboard instantiateViewControllerWithIdentifier:#"SWRevealViewController"];
[self presentViewController:swrController animated:YES completion:nil];
}
My Question to you guys is how can pass data from the blue view controller to the gray (front SWRevealViewController) ?
I'll really appreciate your help.
You have a reference to it with SWRevealViewController *swrController, you can set data on the reference by using information from the presenting view controller.
- (IBAction)gotoGray:(id)sender
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SWRevealViewController *swrController = [storyboard instantiateViewControllerWithIdentifier:#"SWRevealViewController"];
swrController.aProperty = self.myInformation;
swrContoller.anArray = self.arrayOfInfo
[self presentViewController:swrController animated:YES completion:nil];
}
I created a SingleView application then use "Embed In Navigation Controller" to get a navigation control.
when push a controller, the viewcontroller's backgroundColor is black.
i know use this code :
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];//UIStoryboard(name: "Main", bundle:nil)
UIViewController *roomController = [storyBoard instantiateViewControllerWithIdentifier:#"controlerID"];
but what's the reason ?
and if i don't want to use the storyboard completely ,only use this code
UIViewController *roomController = [[UIViewController alloc]init];
what can i do? is only set the viewcontroller's backgroundColor?
You need to use the instantiateViewControllerWithIdentifier: method of your UIStoryboard instance and then you can push the controller.
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *roomController = [storyboard instantiateViewControllerWithIdentifier:#"controlerID"];
[self.navigationController pushViewController: roomController animated:YES];
// if You make controller with out storyboard Than change You view color
UIViewController *roomController = [[UIViewController alloc]init];
[self.navigationController pushViewController: roomController animated:YES];
- (void)viewDidLoad
{
self.view.backgroundColor =[UIColor whiteColor];
}
i am using UINavigationController but when i push a view controller just simply by making object of the class the class load but not the components in it.
this is my AppDelegate.m
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController *vc = [sb instantiateInitialViewController];
UINavigationController *nav = [[UINavigationController alloc] init];
self.window.rootViewController = nav;
nav.viewControllers = #[vc];
and my first UIViewController if action button code is
NextView *next = [[NextView alloc] init];
[self.navigationController pushViewController:next animated:YES ];
it gets to next UIViewController but no internal components are loaded such as - button,textfield,etc.
and if i try this code it give me runtime error
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NextView *next = [sb instantiateViewControllerWithIdentifier:#"two"];
[self.navigationController pushViewController:next animated:YES ];
make sure that NextView inherit UIViewController and you have given identifier as two to that view controller and you have set class NextView from identity inspector to that viewcontroller.
Hope this will help :)
You could not get outlets (buttons etc.) to be loaded with alloc init in the subclass of UIViewController. You should init it with the xib file, using -initWithNibName:bundle: method. Or you can instantiate it with storyboard.
// You can get current storyboard from the View Controller
UIStoryboard *sb = vc.storyboard;
NextView *next = [sb instantiateViewControllerWithIdentifier:#"two"];
[self.navigationController pushViewController:next animated:YES];
I have two storyboard in my project. I am usually can navigate from one View of storyboard to another View of another storyboard?
I am writing code as below
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Details" bundle:nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"Detail"];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
vc.modalTransitionStyle =UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:vc animated:YES];
Now, I have three buttons, and tag on them, How can I pass Custom Object, kept in array to next controller of another storyboard ("DETAIL").
Thanks
Maybe this can help you :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboard2" bundle:nil];
[self presentModalViewController:[storyboard instantiateViewControllerWithIdentifier:#"storyboard2initialviewcontroller"] animated:NO];
EDIT :
Maybe like this, not sure it will help but i ll try :)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"MySegue"]) {
MyOtherViewController *destination = (MyOtherViewController *)segue.destinationViewController;
destination.someProperty = self.someOtherProperty;
}
}
and
UIStoryboard *secondStoryBoard = [UIStoryboard storyboardWithName:#"secondStoryBoard" bundle:nil];
MyOtherViewController *myViewController = (MyOtherViewController *)[secondStoryBoard instantiateViewControllerWithIdentifier:#"myOtherViewController"];
myViewController.someProperty = self.someOtherProperty;
[self.navigationController pushViewController:myViewController animated:YES];
You can pass the custom object like below
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Details" bundle:nil];
HomeViewController *homeVC = [storyBoard instantiateViewControllerWithIdentifier:#"view2"];
// here I am passing string to the another controller (i.e HomeViewController), you can pass any object that you want
homeVC.info = #"This is test string";
[self.navigationController pushViewController:homeVC animated:YES];
Hope this helps you
some days ago I wrote a method to load a view controller using presentViewController:
-(void)passaGC:(NSString *)user
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"generaC"];
[self presentViewController:viewController animated:YES completion:nil];
}
But today I need to pass the variable user from this method to the loaded viewController.
How can I modify my method to do this?
I found other question on stack overflow but nothing is really similar to my request
add a property to your destination viewController (in the .h):
#property (strong, nonatomic) NSString *user;
and finally your method will look like
-(void)passaGC:(NSString *)user
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"generaC"];
viewController.user = user;
[self presentViewController:viewController animated:YES completion:nil];
}
Swift Solution
Except #IBOutlets, you can simply assing data to destination view controller properties.
DestinationVC.swift
var name: String?
SourceVC.swift
let storyboard = UIStoryboard(name: "Helper", bundle: nil)
let destinationVC = storyboard.instantiateViewControllerWithIdentifier("DestinationSID") as! DestinationVC
destinationVC.name = "Mustafa"
presentViewController(destinationVC, animated: true, completion: nil)