开发者

pass data from mainView to a subview

I am building a utility-based application, the data is stored in the MainViewController, and now I know how to pass data to the FlipsideViewController (many regards to this thread BTW, Sending data from Mainview to Flipside?). But I am getting the data onto an subview (subclass of UIView) that I have added to the flipside view. How can I pass data to this subview? I saw there is already a delegate and protocol set up in the FlipsideViewController.h, I am really new to the delegate sort of things. Any help would be greatly appreciated!


Updates:

On the main view, I have a couple of text fields for users to input to create an object. All the objects are stored in an array. Namely, my data is created and stored in the MainViewController. Now on the flip side, I have a custom UIView subclass which allows me to do my own drawing based on the data in that array. What I need to do here is pass the data that stored in MainViewController to this subview. Here is my relevant code:

In the MainViewController.m

- (IBAction)showInfo:(id)sender {  开发者_StackOverflow  

    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
    controller.delegate = self;

    controller.receiver = data;//this is what I've done.

    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];

    [controller release];
}

In the FlipsideViewController.h

@protocol FlipsideViewControllerDelegate;

@interface FlipsideViewController : UIViewController {
    id <FlipsideViewControllerDelegate> delegate;
    DataModel *receiver; //create a property to receive the data transferred from main view
}

@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
@property (nonatomic, retain) DataModel *receiver;
- (IBAction)done:(id)sender;
@end


@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end

In the above code, "data" is an DataModel object declared in the MainViewController.h file.

And I want to do my custom drawing in drawing class (subclass of UIView), how can I pass data from the FlipsideViewControllerto this subview? Do I need to make use of delegate declared in the FlipsideViewController.h file? Thanks in advance!


I have had a quick look at the template and think you are getting confused with what the delegate is being used for.

The delegate in this template is not transferring data. When you have clicked the done button it calls back to MainViewController and asks it to call the dismissModalViewControllerAnimated method so that it can remove the view controller. This seems a bit superflous as the documentation states

If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller.

Therefore you don't really need to call the parent to do this.

In Interface builder you can see that the FlipsideView.xib has it's File's Owner set to FlipsideViewController.xib.

pass data from mainView to a subview

Now if you right click the File's Owner you will see that view is connected to View this basically means that view is the name of the property in FlipsideViewController and View is the element in Interface Builder.

pass data from mainView to a subview

Therefore we can access elements in the xib file from FlipsideViewController using outlets.

To say draw a label you will need to do a couple of things

First add a property in the .h and synthesize it in the .m like

// FlipsideViewController.h
@interface FlipsideViewController : UIViewController

@property (nonatomic, retain) IBOutlet UILabel *testLabel; // <----- Added this
@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;

- (IBAction)done:(id)sender;

@end

// FlipsideViewController.m
@implementation FlipsideViewController

@synthesize delegate = _delegate;
@synthesize testLabel = _testLabel; // <----- Added this

// More methods

- (void)dealloc
{
   [_testLabel release];  // Always do you memory management
   [super dealloc];
}

Then back in Interface Builder

  1. Add a UILabel element to your view
  2. ctrl + drag from File's Owner to the UILabel you added
  3. Select the label in my example it is testLabel

Now these are hooked up correctly. The place where you want to be setting the value of the label is in viewDidLoad: which you can now do like this

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.testLabel.text = @"It Works";  // You would use the data passed in from `MainViewController`
}


I find the easiest way to pass data from one view to another is by directly setting the data in the next view from the original view.

For example;

In your FlipsideViewController.h, declare a 'container' for the data you want to pass. It must be the same class on both sides to work properly, ie. NSArray to NSArray, NSMutableDictionary to NSMutableDictionary.

NSMutableArray *newData;
...
@property (nonatomic, retain) NSMutableArray *newData; // This allows you to access this object from outside this class.

and in FlipsideViewController.m

@synthesize newData;
...
[newData release];

Now we need to pass the data, so to speak. Let's say the data we want to 'send' is stored in a NSMutableArray called 'results'.

In our MainViewController.m, when we are instantiating our next view controller (in this case FlipsideViewController) we can directly reference the newData mutable array after we initalize the nib.

FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
controller.newData = results;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];

Make sure you are importing your FlipsideViewController in your MainViewController.h file.

If the property is declared in your .h file, you can pretty much reference the contents of the object from anywhere within the view stack!

Hope that helps :D

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜