Sending data from Mainview to Flipside?

不羁岁月 提交于 2019-12-24 06:38:34

问题


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?


回答1:


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;
}



回答2:


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?



来源:https://stackoverflow.com/questions/5293957/sending-data-from-mainview-to-flipside

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!