How to pass arguments between view controllers [duplicate] - ios

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 8 years ago.
I'm looking for a way to pass arguments from view controller to another. For example:
I've designed one view controller that receives username and password, and has a submit button which leads to another view controller, which I've designed in main.storyboard. The connection between the controllers is by dragging the button to the second view controller and choosing 'modal' in the connection type.
How can I transfer the user details to the second view controller?

You'll need to use prepareForSegue:sender: method. As taken from https://stackoverflow.com/a/7865100/657676:
- (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];
// or
vc.property = value;
}
}

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!

how to pass tableview from one view controller hide to another view controller unhide using button action

I am new to IOS i created two view controller.First view controller contain tableview as table1 and Second view controller contains button. I want to pass table1 to second view controller and unhide table1 in button action.
pop up for second view controller
first view controller after button action in second view controller
For instance, consider I had two view controllers. The first contains one button and the second needs to know information on the first view . 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
{
// 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];
}
}
For more information you can see this: http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/

Possible to pass data from a ViewController to a Child of another View controller when seguing to the parent of the child? [duplicate]

This question already has an answer here:
How to pass data to a child view controller from a previous segue?
(1 answer)
Closed 7 years ago.
I need to pass some data from a View Controller to a Child of another ViewController(parent) when i segue to the ParentVC
i tried this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// NSLog(#"prepareForSegue: %#",segue.identifier);
ChildVC *transfer = segue.destinationViewController;
if ([segue.identifier isEqualToString:#"Test"]) {
transfer.testLabel = #"Pass this data";
}
}
but its not passing the data over to the child when i segue over to the Parent.
You need to cast the id of segue.destinationViewController to your class ChildVC
Try replacing ChildVC *transfer = segue.destinationViewController; with the following:
ChildVC *transfer = (ChildVC *) segue.destinationViewController;

How to Pass Data Back [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 8 years ago.
This is the scene of my storyboard.
UINavigationController -> UITableViewController -> push segue -> UIViewController
I know how to pass the data of UITableViewController to UIViewController.
But, how can I pass the data back to the UITableViewController?(UIViewController has a navigation bar with a back button)
You can do it through the use of delegation.
When you pass data of UITableViewController to UIViewController, you can set a delegate on UIViewController. Whenever you want to pass data back to UITableViewController, you can call the delegate and pass in the data
Here's a more concrete example in code
- (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
UIViewController *vc = [segue destinationViewController];
// Pass the UITableViewController to the UIViewController
[vc setDelegate:self];
}
}
Create a method on UITableViewController that takes data from UIViewController (example: -(void)processDataFromUIViewController:(NSString *)string)
Now when you want to pass data back to UITableViewController, call [delegate sendDataFromUIViewController:#"data"]

Perform Segue from Root View Controller of a UIPageViewController

I have a UIPageViewController and have a button in it. Whenever the button is pressed I want to perform a Segue from the parent view controller (which has a navigation controller embedded) to the next view controller in the navigation stack. I also want to be able to pass data through the segue. I've tried a couple of things but I'm very new to iOS development and have not been successful at all.
You need to select the segue in your storyboard and give it a unique identifier and the put that in the code below.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"YourSegueIdentifier"]) {
// get a reference to the destination View Controller
UIViewController *destinationVC = [segue destinationViewController];
XXYourViewControllerSubclass *yourVC = (XXYourViewControllerSubclass*)destinationVC;
// create the data and pass it to the view controller
id someData = // create your data (unless you have a property holding it already)
[yourVC acceptData:(id)someData]; // make a public method on your VC subclass to accept the data
}
// after this method has returned the seque will be performed
}
Does that make sense?

Resources