Reputation: 713
I need to pass data between different views in my application. The different views are used to set properties of a class that is used throughout the application.
I have somehow managed to achieve this by passing copies of the class around the views and setting the corresponding properties when the view in question is closed, but I am not sure this is the best way to achieve what I need to do.
See below my pseudo code:
Car Class:
@interface Car : NSObject
{
id delegate;
NSString *carModel;
NSString *carColor;
BOOL turboCharged;
int carEngineSize;
NSString *carFuelType;
}
@property [...]
MainView implements a myCar class that is used throughout the application.
CarSettingsView:
Implements a dummyCar Car.
@class Car;
@interface SettingsPopOver : UITableViewController <UIPopoverControllerDelegate>
{
NSMutableArray *tableContentsArray;
UINavigationController *navigationController;
SetParametersView *paramView;
Car *dummyCar;
}
When Calling CarSettingsView from the MainView, I pass over myCar :
- (IBAction)btnSettings:(id)sender {
settings = [[SettingsPopOver alloc]init];
[settings setDummyCar:myCar]; //Pass over myCar from the MainView
UIPopoverController *aPopOver = [[UIPopoverController alloc]initWithContentViewController:settings];
//self->popOverController = aPopOver;
popOverController = aPopOver;
popOverController.delegate = self;
[popOverController setPopoverContentSize:CGSizeMake(300, 400)];
[popOverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
I then perform any property changes directly on dummyCar in the settings view, then copy dummyCar back into myCar at the dismissal of the settings view:
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
myCar = [settings dummyCar];
}
That works OK for me. But I am not sure it is the right way, also it becomes a bit awkward when multiple settings views are nested and the object needs to be passed down several layers and then back up to the main view where the initial object resides.
Happy to hear comments on this, and/or online links that would put me in the right direction.
Upvotes: 0
Views: 158
Reputation: 23550
Getting the dummyCar property for the settings controller at the end is not a good idea. But two good ways of doing are very close of what you are doing :
Idea 1 - Make dummyCar as an "assign" or "retain" property. Doing that way, you will be able to change the car object properties directly from the settings controller. No need to do it in the main caller view at the end.
That will lead to a way of doing that is like the one you can see when setting your iDevice settings : no need to ask the user to save. Any change is directly taken into account.
@property(nonatomic, retain) Car *dummyCar; // .h
@syntesize dummyCar; // .m
Idea 2 - Make the dummyCar as a "copy" property (best). That way the original car is not modified by your changes. Do what you need, and in the method you call on the main caller at dismissal time, pass the modified car object you want to replace the original car. Then do that replace.
@property(nonatomic, copy) Car *dummyCar; // .h
@syntesize dummyCar; // .m
Implement NSCopying protocol my Car
@interface Car : NSObject <NSCopying> // .h
Implement copyWithZone // .m
and
// Protocol method on main caller called by settings controller at dismissal time
-(void)popoverControllerDidDismissPopoverWithNewCar:(Car*)modifiedCar
{
self.myCar = modifiedCar;
}
Upvotes: 0
Reputation: 6401
I haven't read your entire question. " But I am not sure it is the right way, also it becomes a bit awkward when multiple settings views are nested and the object needs to be passed down several layers and then back up to the main view where the initial object resides." Looks like you are looking to share value across multiple view controllers or multiple classes... then Singletons are way to go. http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html this article written by Matt Gallagher explains about it.
Upvotes: 1