In prepareforsegue how datas are passed by using arguments - ios

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

Related

Need some insight to make a 3 UIButton menu in XCode that toggles and changes dataset in the main ViewController

I'm new on Stackoverflow and I'm currently learning XCode from scratch and I'm in a process of making a Single Page Application with options.
Anyone knows how to efficiently make a simple menu with multiple selectable UIButtons that make the main ViewController display different datasets depending on the selection in XCode?
Tried different things (creating SecondViewController for example but can't figure out how to pass data from it to main ViewController).
Any help will be greatly appreciated.
Refer this:
https://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
How to pass prepareForSegue: an object
Simple Test from above Answer Reference:
Simply grab a reference to the target view controller in prepareForSegue: method and pass any objects you need to there. Here's an example...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
REVISION: You can also use performSegueWithIdentifier:sender: method to activate the transition to a new view based on a selection or button press.
For instance, consider I had two view controllers. The first contains three buttons and the second needs to know which of those buttons has been pressed before the transition. You could wire the buttons up to an IBAction in your code which uses performSegueWithIdentifier: method, like this...
//When any of my 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];
}
}
Hope this help but please go through refernces!

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

Master-Detail using Parse

Then I have my MasterViewController with its DetailViewController segued both.
I'm using Parse for backend platform, and I have a little problem for this: How to pass displaying datas of the MasterViewController to the DetailViewController
I'm using a NSArray called "RetrievingObjects" to retrieve it, For my Parse class querying it works successfully (I already used my query and my Array to display cells in MasterViewController), I use that method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailVc = [self.storyboard instantiateViewControllerWithIdentifier:#"detailVC"];
detailVC.lblDescription = [self.RetrievingObjects objectAtIndex:indexPath.row];
detailVC.lblTitleForDescr.text = [self.RetrievingObjects objectAtIndex:indexPath.row];
detailVC.dateForDescr.text = [self.RetrievingObjects objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:#"goToDetailSg" sender:self];
}
It looks like you're not actually using detailVc anywhere. You should set it up in MasterViewController's prepareForSegue:sender: You can access what will become the DetailViewController through the segue object's destinationViewController property and set everything you need there.
Don't create the detail controller in code. If you have your segue set up correctly in the storyboard, that work will be done for you.
Instead, implement - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender, get the detail controller from the segue parameter, and pass the information you want it to display in that method.
Note that the destination controller's view will not have loaded at that point, so don't try to update IBOutlets.

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.

prepareForSegue is passing value by Reference to Destination view Controller

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

Resources