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 Main开发者_如何学运维view to Flipside?
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;
}
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?
精彩评论