1

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.

Tim Moth
  • 11
  • 2

1 Answers1

0

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!

Community
  • 1
  • 1
nikdange_me
  • 2,949
  • 2
  • 16
  • 24