views:

2297

answers:

4

Hello guys,

I have following problem:

I have built a tabbar application with 4 tabs. I want to pass a object/variable from the first tab controller to the third one and initialize this controller with the corresponding object.

I've already done some research. The best way, corresponding to a clean model approach, would be to call some initWithObject: method on the called viewcontroller. How can I achieve this? How can I call the init method of the receivercontroller within the callercontroller? Can you give me some code example?

Thanks in advance.

Edit: To pass data between several views/classes etc simply create some Kind of data class which holds the data beeing shared between several classes. For more information follow the link: Singleton

Greetz

+1  A: 

You might want to consider NSNotificationCenter (Reference); you register the one viewcontroller with the application notification center, and send a notification when a selection is made. When the notification is received, the other viewcontroller updates itself accordingly.

Williham Totland
Thats true, but i think you cannot pass objects through NSNotificationCenter. So i could send some Notification to the receiverController. But then i still have the problem that i need my init parameters to initiate the new viewcontroller.
Simon D.
You would be wrong. You can perfectly well pass objects through `NSNotificationCenter`. You can use, for example; `- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo`, or just `+ (id)notificationWithName:(NSString *)aName object:(id)anObject`
Williham Totland
A: 

I don't think this is best practice (also check syntax) however I have got away with:

in the .h

otherclassref *otherclassname

@property (assign) otherclassname otherclassref;

and in the .m

@synthesize otherclassref;

then I just assign the reference from somewhere convenient e.g. the app delegate or wherever you are instantiating your viewcontrollers.

then the view controller can get a reference to the other view controller.

I add @class secondviewcontroller to the .h file for the firstviewcontroller and put put the #imports "secondviewcontroller.h" in the .m file of the first view controller. These are called forward references and prevent compiler errors resulting from having .h files referencing each other.

PeanutPower
This will work but it is not good practice because the complexity of the references between the view controllers will snowballl as you add views. Adding or removing one view controllers means updating several others. One view controller can mangle the data held in another.
TechZen
@TechZen yep sounds reasonable to me
PeanutPower
+2  A: 

You need a data model object that stores the data for application.

A data model is a customized, standalone object accessible from anywhere in the application. The data model object knows nothing about any views or view controllers. It just stores data and the logical relationships between that data.

When different parts of the app need to write or read data, they right and read to the data model. In your case, view1 would save its data to the data model when it unloads and then view2 would read that data from the data model when it loads (or vice versa.)

In a properly designed app, no two view controllers should have access to the internal data of another controller. (The only reason a view controllers needs to know of the existence of another controller is if it has to trigger the loading of that other controller.)

The quick and dirty way to create a data model is to add attributes to the app delegate and then call the app delegate from the view controllers using:

MyAppDelegateClass *appDelegate=[[UIApplication sharedApplicaton] delegate];
myLocalProperty=appDelegate.someDataModelProperty;

This will work for small project but as your data grows complex, you should create a dedicated class for your data model.

Edit:

To clarify for your specific case, you would add the call to the data model when the receiver viewController becomes active.

Placing the data in an init method or a viewDidLoad won't work because in a tabbar the users can switch back and forth without unloading the view or reinitializing the view controller.

The best place to retrieve changing data is in the viewWillAppear controller method. That way the data will be updated every time the user switches to that tab.

TechZen
Thanks a lot. I use now the singleton approach. I've also posted some link to a good tutorial which describes, which steps have to be done to create a singleton data model.
Simon D.
A: 

Could you write below code in more detail?? How will I specify the type of "myLocalProperty"? where I will create "MyAppDelegateClass" in app delegate?

MyAppDelegateClass *appDelegate=[[UIApplication sharedApplicaton] delegate]; myLocalProperty=appDelegate.someDataModelProperty;

just follow the link i've added to my initial post.
Simon D.