1

I'm still having a hard time with this data encapsulation thing...

I know that the Flipside thing is supposed to be for settings and what not, and that sending data from flipside back to mainview is easy (for some) or maybe even built in. Is there something about that flipside/mainview template that makes it difficult or impossible to send data from Mainview to Flipside?

Sam
  • 125
  • 1
  • 7

2 Answers2

2

In the Utility Template, the MainViewController (the view controller for the front side) creates the FlipsideViewController (the view controller for the back side) in the showInfo: method. You can pass whatever data you like to the FlipsideViewContrller constructor (provided, of course, that you change the constructor to accept the data).

Alternatively, you could define some properties in FlipsideViewController. After you create the object (again in showInfo:) you can then set those properties with the data you want to pass.

Edited per @Sam's comment:

In FlipsideViewController.h, you'd define a property to hold the data you want to show. Here, I'm making it an NSString but it can be anything.

@property (nonatomic, retain) NSString *someDataToShow;

In MainViewController.m, after you create the FlipsideViewController, you'd set the property. Note, this assumes you have a method computeDataToPassToFlipside defined in MainViewController that returns an NSString.

- (IBAction)showInfo:(id)sender
{    
    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
    controller.delegate = self;
    controller.someDataToShow = [self computeDataToPassToFlipside];
    // ...
}

Finally, in FlipsideViewController.m you need to do something with the data you have passed to the property. So, for example, let's say you have a UILabel called myLabel that you want to have display the NSString property. I'm going to assume the UILabel is correctly included and attached to an IBOutlet using Interface Builder.

- (void)viewWillAppear:(BOOL)animated
{
    myLabel.text = self.someDataToShow;
}
pwc
  • 7,043
  • 3
  • 29
  • 32
  • I'm terrible at the conceptual stuff. I have to see code to understand it. Lets say I wanted to add A + B = C in some method in the mainview, and then pass the value of C to be displayed on the flipside. How would I go about doing that? – Sam Mar 14 '11 at 03:37
  • OK, I added some more details to my answer. Hope it helps. – pwc Mar 14 '11 at 04:37
  • Why does it have to be a function computeDataToPassToFlipside? Why can't I just controller.someDataToShow = myFunkyString, where myFunkyString has already been determined by other parts of the program? – Sam Mar 14 '11 at 22:00
  • That works, too. I didn't know if you had an ivar with the data or you were computing it. – pwc Mar 14 '11 at 22:58
  • You forgot to tell me to @synthesize someDataToShow; See, that's what happens when people assume I'm not an idiot. Seriously though, after much head banging against a keyboard, I had a DUH moment and figured it out. Thanks a ton for the code!! – Sam Mar 15 '11 at 02:47
1

The Utility template has a delegate built for you already.

For some good info on delegates, look at this question:

How does a delegate work in objective-C?

Community
  • 1
  • 1