prepareForSegue is passing value by Reference to Destination view Controller - ios

I am using storyboard in one of my application and while passing a property from SourceViewController to DestinationViewController in prepareForSegue method the property being passed is passed by reference instead of passed by value. Which is causing some trouble for me as in Destination View Controller if user fiddles with the values and taps on back then I get an updated value in my SourceViewController which I do not want.
Please see the following to get more information on the issue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{if([[segue identifier] isEqualToString:#"DestinationView"])
{
DestinationViewController *destinationViewController = [segue destinationViewController];
[destinationViewController setActivity:activity];
//Here the value of activity is passed by reference. I have verified the same by checking the address of activity variable in Variable pane of Xcode in Source and destination view controller
}
Now I am not sure how to only pass the value and not the reference in the above case.

try to use a copy of it
[destinationViewController setActivity:[activity copy]];
Implement its delegate copywithZone if needed
- (id)copyWithZone:(NSZone *)zone

Related

Passing data to destination view controller with performSegue

So I know that if there is a segue which is setup and activated through the storyboard, one can use the prepareForSegue method to pass data to the destination view controller.
I'm wondering if there is a way to do this if one uses the performSegueWithIdentifier method? It seems the prepareForSegue method is only called if the segue is activated with the storyboard, not with performSegue.
One particular problem is that I can't access the UIStoryboardSegue (it is not an argument in performSegue as it is in prepareForSegue), and therefore can't access the destination VC either.
Answer is here in StackOverflow.
How to pass prepareForSegue: an object
// When any of buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"MySegue" sender:sender];
}
// This will get called too before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MySegue"]) {
// Get destination view
SecondView *vc = [segue destinationViewController];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
// Pass the information to your destination view
[vc setSelectedButton:tagIndex];
}
}

transfer several variables to another ViewController

Tell me, please, how should i transfer several variables from one ViewController to another?
For example, i have
ViewControlle.m
- (void) transferVar{
int a = 10;
int b = 11;
}
and i want to transfer them to another ViewController (SecondViewController.m). What should i do?
you can add a property on the second view controller for every variable you want to pass (or an array to contain them all), then if you are making the segue on the storyboard use the method
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
and inside it make this
SecondViewController *svc = (SecondViewController *) segue.destinationViewController;
There you have the view controller you want to pass values so only save it
svc.yourArray = #[[NSNumber numberWithInt:a],[NSNumber numberWithInt:b]];
Also it is highly recommendable to add a segue identifier to the storyboard segue and check
if([segue.identifier isEqualToString: #"theIdentifier"])
to be sure it is the segue that you wanted
Now on the viewDidLoadMethod you can use them.
If you are not using the storyboard segue just assign the value after initiating the view.

In prepareforsegue how datas are passed by using arguments

In storyboard I want to pass the datas from one viewcontroller to another but only the properties are shared but how can i share the datas by using the arguments(method).In below I have tried like this
Tableview didselect Method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:#"ASEventDetail" sender:self];
}
In prepare for segue I pass the argument value in method "[vc loadDict:[eventArr objectAtIndex:0]]"
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"ASEventDetail"]){
// Get reference to the destination view controller
ASEventDetail *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc loadDict:[eventArr objectAtIndex:self.view.tag]];
}
}
Could you please any one help me to pass the datas from one to another using methods from storyboard.
You can pass data in via properties or methods (as long as that method is storing the data). However, you shouldn't be updating the UI in your loadDict method.
Instead, save your data in a property or instance variable, then in viewWillAppear on the destination controller, update the UI based on the data.
Set your custom properties on the destination view controller,Use this method to pass the property in all of your view controllers
ASEventDetail *vc = [segue destinationViewController];
vc.someobject=[eventArr objectAtIndex:self.view.tag];
for reference you can refer Pass Data Between View Controllers

Can't access parentviewcontroller's properties

Here's a quick and dirty question:
I have a "main" view controller (VC) which is opened up from a parent VC, that uses a navigation controller. Then I have a "sub" VC that is opened (modal segue) from the "main" VC.
I have set a property in the main VC's interface:
#property (nonatomic) int myVar;
Then set it from the button's action that is touched to display the "sub"VC from the "main"VC's interface:
self.myVar=1;
I imported the mainVC.h in the subVC.h
Then at the viewDidLoad method of the subVC, I'm trying to access myVar's value, but can't do that with:
NSLog(#"Myvar is %i", ((mainVC*)self.parentViewController).myVar);
Which returns the value as 0.
And when I try presentingViewController method instead, I get an error (which did not cause the error when I pushed the segue instead of making it a modal:
[MainVC myVar]: unrecognized selector sent to instance
I'm trying to code for iOS 5, and needless to say that I'm still a noob.
What am I missing?
Thanks!
The parentViewController of the "sub" view controller is not the mainVC, it's the navigation controller. The mainVC is not accessible - for all you know, it may be deallocated to save memory.
If you need to pass data from the main controller to the sub controller on the segue, add an instance variable to the "sub" view controller, and set it in the prepareForSegue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"open_sub"]) {
subVC *sub = segue.destinationViewController;
sub.myVar = 1;
}
}

iPhone app crashes when passing data in prepareForSegue:sender

I've been trying to pass data from one tableViewController to another, but as soon as I assign a value to a property in my second tableViewController the app crashes, giving me the message "...unrecognized selector sent to instance 0x715c340...".
Here is the code I have used:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"displayWordlist"]){
DisplayWordlist *dwl = (DisplayWordlist *)[segue destinationViewController];
dwl.apa = #"hej";
}
}
As soon as the program reaches 'dwl.apa = #"hej";' it crashes. I have both made a property of apa and synthesized it.
Make sure you have set the class of this view controller to DisplayWordlist in the Storyboard. If you don't, dwl will be assumed to be an instance of UIViewController despite the cast.

Resources