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 {
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 FlipsideViewController
to 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 thedismissModalViewControllerAnimated
method so that it can remove the view controller. This seems a bit superflous as the documentation statesTherefore you don't really need to call the parent to do this.
In Interface builder you can see that the
FlipsideView.xib
has it'sFile's Owner
set toFlipsideViewController.xib
.Now if you right click the
File's Owner
you will see thatview
is connected toView
this basically means thatview
is the name of the property inFlipsideViewController
andView
is the element in Interface Builder.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
Then back in Interface Builder
UILabel
element to your viewctrl + drag
fromFile's Owner
to theUILabel
you addedtestLabel
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 thisI 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.
and in FlipsideViewController.m
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.
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