Access property of ViewController in Storyboard - ios

NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"drawingboard"];
I want to access vc.property but the property I want to access in not accessible even though the property is in .h file and has a nonatomic, assign property. Is there anyway I can access that? Please help! Thanks in advance.

The vc must be casted to a specific class which is a subclass of UIViewController.
For instance:
Drawingboard *vc = [storyboard instantiateViewControllerWithIdentifier:#"drawingboard"];

If you define the property in the .h file than you don't need to redefine it in the .m. Otherwise you'll be accessing the .m object and not the .h property.
#property (nonatomic) UIViewController *vc;
Above would be your header, below is the implementation
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
_vc = [storyboard instantiateViewControllerWithIdentifier:#"drawingboard"];
_vc can also be replaced with self.vc depending on preference but that would be how you access it later on. Hope this Helped

Implement prepareForSegue: method as below:-
Lets say CommentsViewController is the class name of your viewController whom you want to push.
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.destinationViewController isKindOfClass:[CommentsViewController class]]) { // Your viewControllerclass
CommentsViewController *controller = [segue destinationViewController];
controller.propertyName = #"Write value here";
}
}

Related

Transition from XIB to Storyboard with data

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];

How to pas data between view controller without using segue in ios?

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)
}
}

How to custom object between two storyboard in one project?

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

Passing Data with IBAction

I'need to pass data from a UIviewController to another UIviewController with an IBAction because I want to implement a custom transition effect.
to show the next view I use this code, it work perfectly:
NSString * storyboardName = #"MainStoryboard";
NSString * viewControllerID = #"NextViewController"; // si imposta nello storyboard, subito sotto la classe di appartenenza nell'identity ispector
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
NextViewController * controller = (NextViewController *)[storyboard
instantiateViewControllerWithIdentifier:viewControllerID];
[self passData]
To pass data I tried what I usually do in prepareForSegue, so assign a property to NextViewController.h
#property (assign,nonatomic) int indiceDue;
and use that property to pass data
-(void) passData
{
UIStoryboardSegue* segue = [[UIStoryboardSegue alloc]init];
IbaViewController*dvc = segue.destinationViewController;
dvc.indiceDue = self.indice;
}
The problem is that it don't work. How can I solve my problem?
Call this method inside the ibaction:
NextView*dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"NextView"];;
dvc.indiceDue = self.indice;
[self.navigationController pushViewController:dvc
flipStyle:MPFlipStyleFlipDirectionBit(MPFlipStyleOrientationVertical)];// or the transition that you like

Pass data between view controllers WITHOUT segues

I'm using storyboard and I have a view that loads dynamic buttons. On click of this button, I need to load the second view controller and also pass data. I cannot use segue as its dynamic button.
I use following code to load the second view controller
UIStoryboard* sb1 = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone"
bundle:nil];
UIViewController* vc1 = [sb1 instantiateViewControllerWithIdentifier:#"SecondViewController"];
[self.navigationController pushViewController:vc1 animated:YES];
In the 2nd view controller, I need to access about 5 fields from the 1st view controller. How do I pass the data?
You can create a Segue not bounded to a button by ctrl+drag from the first controller to the second one (don't forget to give this segue and identifier).
Next In the IBAction of the button (set via Interface Builder or via addTarget:self action:forControlEvents: ) you can call the [self performSegueWithIdentifier:#"YourSegueIdentifier" sender:button];
You can pass the data to the second controller, as usual, in - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
Try something like that:
Class A
// CLASS_A.h
#import <UIKit/UIKit.h>
#interface CLASS_A : UIViewController {
...
}
- (IBAction)Btn_PushPressed:(id)sender;
...
#end
// CLASS_A.m
#import "CLASS_A.h"
#import "CLASS_B.h"
#implementation CLASS_A
- (IBAction)Btn_PushPressed:(id)sender
{
UIStoryboard* sb1 = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone"
bundle:nil];
CLASS_B* vc1 = [sb1 instantiateViewControllerWithIdentifier:#"SecondViewController"];
vc1.delegate = self;
[self.navigationController pushViewController:vc1 animated:TRUE];
}
....
#end
Class B
// CLASS_B.h
#import <UIKit/UIKit.h>
#class CLASS_A;
#interface CLASS_B : UIViewController {
...
}
#property (weak, nonatomic) CLASS_A *delegate;
....
#end
// CLASS_B.m
#import "CLASS_B.h"
#import "CLASS_A.h"
#implementation CLASS_B
....
#end
This is how to do it in Swift 2.0. Instead of using prepareForSegue,
Swift 3.0
let someViewConroller = self.storyboard?.instantiateViewController(withIdentifier: "SomeViewController") as! SomeViewController
someViewConroller.someObject = self.someObject!
self.navigationController?.pushViewController(someViewConroller, animated: true)
Swift 2.0
let someViewConroller = self.storyboard?.instantiateViewControllerWithIdentifier("SomeViewController") as! SomeViewController
someViewConroller.someObject = self.someObject!
self.navigationController?.pushViewController(someViewConroller, animated: true)
Swift 3
let destinationView = self.storyboard?.instantiateViewController(withIdentifier: "ID") as! ViewController2
destinationView.Value2 = Value1
self.present(destinationView, animated: true, completion: nil)
You can simply use
NSUserDefaults
to store the data and retrieve in what ever page you need
In view controller A
[[NSUserDefaults standardUserDefaults] setObject:aData forKey:#"DATA"];
[[NSUserDefaults standardUserDefaults] synchronize];
In destination Controller
NSData * aData = [[NSUserDefaults standardUserDefaults] valueForKey:#"DATA"];
firstly you can use segue even you have dynamic buttons, just add target method to UIButtons and push view controller using segue in that method. in the second view controller, take few properties and assign values to those properties before you push second view controller. like:
UIStoryboard* sb1 = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone"
bundle:nil];
UIViewController* vc2 = [sb1 instantiateViewControllerWithIdentifier:#"SecondViewController"];
[vc2 setName:#"name"];
[self.navigationController pushViewController:vc2 animated:YES];
before that you need to assign property to a NSString *name in SecondViewController
You can define a protocol: PassValueDelegate,and in the first viewController you can implement this protocol.
UIStoryboard* sb1 = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController* vc1 = [sb1 instantiateViewControllerWithIdentifier:#"SecondViewController"];
//set the delegate for the second view controller,so you can access the values in the first controller
vc1.delegate = self;
[self.navigationController pushViewController:vc1 animated:YES];

Resources