Passing Data with IBAction - ios

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

Related

Access property of ViewController in Storyboard

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";
}
}

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

Set Label text equals to variable

I'm writing a program for fun, and in it I have a UIViewController with four buttons, they all send the user to another view, but depending on what button is pressed, I want the label in that other view to have a different text. To do that, I decided to have the label's text to be set equals to a NSString variable, and that variable is set to a certain text depending on what button is pressed. So I have this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"resultScreen"];
[self presentViewController:vc animated:YES completion:nil];
_myLabel.text = [NSString stringWithFormat:myText];
In which myText is the NSString variable I want the label to be set to.
Everything seems to be in order, but the label won't have it's text changed. If I try to change it's text inside the viewDidLoad function, though, it works just fine, so I don't know what is happening.
Any idea of what I might need? If you need more info, just comment what you want and I will try to provide it.
You cannot update UI of other controller from current one - you can set variables and later on in viewDidLoad or viewWillAppear you can assign those values to UI
In File: FirstViewController.m
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SecondViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
vc.passedText = #"Hello Next";
[self.navigationController pushViewController:vc animated:YES];
In File: SecondViewController.h
#interface SecondViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *myLabel;
#property (strong, nonatomic) NSString *passedText;
In File: SecondViewController.m
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.myLabel.text = self.passedText;
}
I think you need to set your ViewController's property before calling the:
[self presentViewController:vc animated:YES completion:nil];
If you set it after presenting the view, you have to manually refresh the view, which is a bit of an overkill. Just set it before presenting the view. So it would look like:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ResultScreenViewController *vc = (ResultScreenViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"resultScreen"];
vc.customTitleLabel.text = [NSString stringWithFormat:myText];
[self presentViewController:vc animated:YES completion:nil];
In your ViewController header file:
#property (nonatomic, strong) UILabel *customTitleLabel;
First Create a subclass of a UIViewController with a custom label:
#interface NextViewController : UIViewController
#property (nonatomic, strong) UILabel* customLabel
#end
Now back to your code. You simple cast the UIViewController you get to the subclass you created so you can access the label inside you view controller.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NextViewController* nextVC = (NextViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:#"resultScreen"];
nextVC.customLabel.text = [NSString stringWithFormat:myText];
[self presentViewController:nextVC animated:YES completion:nil];
Ok, with the help of one of the answers here I found I solution that works for me. Before, when I had
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"resultScreen"];
[self presentViewController:vc animated:YES completion:nil];
_myLabel.text = [NSString stringWithFormat:myText];
I didn't realise that the resultScreen wasn't linked to UIViewController, but to ViewContronller. So all I needed to do was change the vc declaration to
ViewController *vc = (ViewController *)[mainStoryboard instatiateViewControllerWithIdentifier:#"resultScreen:];
So then all I needed to do was change the label's text at the ViewDidLoad module. It works just fine for me now.
Its simple..
if ([_mylabel.text isEqualToString: myText)
{
//--- Now process your method here. Show alert or what else you need
}
try this. I think that accessing UIlabel directly of other UIViewController is not a good approach. Follow this & it will definitely work.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
resultScreen *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"resultScreen"];
vc.strText = myText;
[self presentViewController:vc animated:YES completion:nil];
In your resultScreen view controller create a NSString property.
resultScreen.h
#property (retain, nonatomic) NSString *strText;
resultScreen.m
-(void)viewDidLoad
{
self.myLabel.text = self.strText;
}
you are trying to set the labelText before presenting the second View.
Before view gets loaded into the memory, its all outlets are nil.
so use the NSString variable in second view, and try to set the value of that variable,and then in
viewWillAppear: method assign the value of string variable to the labelText, this will definitely work.
Cheers!

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