2

I'm trying to learn how to pass data between views. Say set a label in the second view from text entered into a text field on the first view. I basically have tried making a string in the second view and then when switching from the first view to the second I set a string in the second view. Then when the second view loads its sets the text of a label to the same string. I NSLog right before and after the transition, before its fine, but when the second view loads it string gets erased. I'm not sure why this isn't working. Here is my project: http://www.mediafire.com/?83s88z5d06hhqb5

Thanks!

-Shredder2794

Praxder
  • 2,315
  • 4
  • 32
  • 51

6 Answers6

2

From my book (http://www.apeth.com/iOSBook/ch19.html#_storyboards):

Before a segue is performed, the source view controller is sent prepareForSegue:sender:. The view controller can work out what segue is being triggered by examining the segue’s identifier and destinationViewController properties, and the sender is the interface object that was tapped to trigger to the segue (or, if performSegueWithIdentifier:sender: was called in code, whatever object was supplied as the sender: argument). This is the moment when the source view controller and the destination view controller meet; the source view controller can thus perform configurations on the destination view controller, hand it data, and so forth.

(Of course another solution is "don't use a storyboard". Then the first view controller creates the second and can hand it data then and there.)

The reverse problem is much trickier; look at the Utility Application template for an example of how to use the delegate pattern.

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

StoryBoards are ready made things where you can reduce a lot of code you write.So consider controller A & B on storyboard. Now for passing data From A to B you can connect them with a segue name its identifier and then you can use delegate methods in A as:

//This method gets called before transition from A to B.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([[segue identifier] isEqualToString:@"THE IDENTIFIER YOU NAMED"])
    {

        id *objectOfController_B  = [segue destinationViewController];.
        objectOfController_B.lblTextDisplayOfA = //Something...
    }
}

Now You can Explicitly transition it by using button in controller A.

- (IBAction)buttonPressed:(id)sender
{
    [self performSegueWithIdentifier:@"THE IDENTIFIER YOU NAMED" sender:sender];
}

So I guess you can try experimenting on this and you will get it how and when transition occurs with segue.

Hope this helps.

Vish
  • 341
  • 1
  • 4
  • 19
0

I spent "countless hours" my self trying to find a way to pass data and understand delegates with no comprehension and very little success. This video did something that all the other references I checked didn't do : keep it as simple as possible while clearly showing what was needed. Thank you so very much Mr Rob Smythe. http://www.youtube.com/watch?v=XZWT0IV8FrI

Carter
  • 27
  • 7
0

Have a look at this: http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

Delegate pattern is a common way to achieve what you are trying to do.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kasper Munck
  • 4,173
  • 2
  • 27
  • 50
0

There appear to be more than a few things to explain. I think working through a few tutorials will give you the answers you need. See http://www.raywenderlich.com/tutorials

T.J.
  • 3,942
  • 2
  • 32
  • 40
0

i've asked more or less the same question a few weeks/month ago. there were some very good answers, especially the one from zoul, who built a demo project that will show you how to create a factory pattern application that will provide the views with the needed objects.

my question can be found here: iOS: Initialise object at start of application for all controllers to use and have a look at the answer from 'zoul'. it got me through this problem =)

good luck trying it out =) sebastian

Community
  • 1
  • 1
Sebastian Flückiger
  • 5,525
  • 8
  • 33
  • 69