PDA

View Full Version : Passing values to another view - Utility Template




Nraygun
Jan 20, 2009, 06:39 AM
Using the Utility Application template, I'm now trying to setup controls on the flipside that control the app on the mainview.

I figured out that I needed to create more instance variables in the FlipsideViewController and I think I need a variable for the control and another to store the value(instead of using the control values directly per the Design pattern recommendation). I think this will adhere to the Model-View-Controller design pattern.

How do I access these variables in this MainView?

I've tried flipsideViewController.var with no luck. I also added "import FlipsideViewController.h" in MainViewController.

Any ideas?



jeremy.king
Jan 20, 2009, 10:45 AM
I used a singleton object with exposed properties for storing data used across views.

Nraygun
Jan 21, 2009, 10:28 PM
OK, I found the code for a Singleton in the Apple Documentation(Cocoa Fundamentals).

I inserted a folder in my project and created the appropriate .h and .m files for this singleton object.

So, now do I just add instance variables to this object to store the various data for my app that needs to be shared?

Then, how do I access them? It seems like I'd have to create a dummy variable in each area I want to use the Singleton. Since it only allows one instance of itself, it should only use the same one across the views. Right?

I tried this:
MySingleton *ptrtosingleton = [[MySingleton alloc] init];
a = prttosingleton.aninstancevariable;

where a is a local variable and aninstancevariable is the instance variable that would be the same data type as a.

Does this sound correct?

Luke Redpath
Jan 22, 2009, 02:37 AM
A singleton doesn't sound like the answer here; its a pattern thats easy to abuse. Store the data in some kind of model object and pass that into the controllers that need to use it.

caveman_uk
Jan 22, 2009, 02:58 AM
I tried this:
MySingleton *ptrtosingleton = [[MySingleton alloc] init];
a = prttosingleton.aninstancevariable;

You're defeating the point of a singleton. The point of a singleton is there is only one instance...ever. They are usually accessed by using

[[MySingleton sharedInstance] someMethod]

I do have to agree that maybe a singleton isn't what you really want here. Without knowing what you really want to share between classes it's hard to recommend but given that the flipside is usually used for settings maybe user defaults is what you need to use.

Nraygun
Jan 22, 2009, 06:47 AM
Thanks everyone. I'm just trying to do implement something that seems like it's the way Apple recommends. I'd much rather use other means, but it seems like this is the correct route.

As far as what I'm trying to do, I'm just trying to have some basic settings be accessible throughout the app. These settings would be set in the flipside and then utilized in the mainview. Like the Weather app, the Stocks app, etc. - these have setup options on the flipside that affect what the mainview does.

For example, maybe I want to set a color for something in the mainview. I'd add a control to the flipside for the user to pick the color, store it somewhere, and then the mainview would read that value from somewhere to set the color for, say, a label.

Another question would be - does anyone know where I can find the source code for a simple application that uses the Utility Application template?

Either way, I hope you guys can help.

tsornin
Jan 22, 2009, 08:23 AM
As far as what I'm trying to do, I'm just trying to have some basic settings be accessible throughout the app. These settings would be set in the flipside and then utilized in the mainview. Like the Weather app, the Stocks app, etc. - these have setup options on the flipside that affect what the mainview does.

There are various ways to do this. You could store them in NSUserDefaults (you'll probably want to do that anyway to maintain the setting between app launches).

You could add properties to the app delegate and set/retrieve them from other controllers (I don't know how MVC that is, but I use it):

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate setSettingVar:@"somevalue"];

You could also use NSNotification to post a notification when some value changes, then your other controllers can watch for those notifications and react appropriately.

jeremy.king
Jan 22, 2009, 09:09 AM
A singleton doesn't sound like the answer here; its a pattern thats easy to abuse. Store the data in some kind of model object and pass that into the controllers that need to use it.

Having to pass around an object can become difficult to manage, depending on the number of views/view controllers, and especially for new developers. This is why I suggested singleton. This comes at a cost that any object can modify the model, but with a benefit of ease of implementation.

As explained above, there are many ways to share data.

Nraygun
Jan 22, 2009, 07:36 PM
Thanks again everyone.

It seems like I'm still a little confused on how to setup and utilize a Singleton.

Using the AppDelegate and NSUserDefaults might work, but I think I'd like to try the Singleton.

I inserted the code from Apple's documentation into a new header and implementation file respectively.

When I try to use the Singleton from another class, I get compiler errors saying that the methods are not setup. I've added the appropriate property and synthesize directives to create the accessors for the variables I've added to the class.

I tried Googling for more examples but I can't find any.

Do you have any further suggestions?

dejo
Jan 22, 2009, 07:53 PM
When I try to use the Singleton from another class, I get compiler errors saying that the methods are not setup
What methods are you trying to access?