Passing values to another view - Utility Template

Discussion in 'iOS Programming' started by Nraygun, Jan 20, 2009.

  1. Nraygun macrumors newbie

    May 9, 2005
    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?
  2. jeremy.king macrumors 603


    Jul 23, 2002
    Fuquay Varina, NC
    I used a singleton object with exposed properties for storing data used across views.
  3. Nraygun thread starter macrumors newbie

    May 9, 2005
    Singelton use help

    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?
  4. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    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.
  5. caveman_uk Guest


    Feb 17, 2003
    Hitchin, Herts, UK
    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.
  6. Nraygun thread starter macrumors newbie

    May 9, 2005
    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.
  7. tsornin macrumors newbie

    Jul 23, 2002
    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.
  8. jeremy.king macrumors 603


    Jul 23, 2002
    Fuquay Varina, NC
    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.
  9. Nraygun thread starter macrumors newbie

    May 9, 2005
    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?
  10. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    What methods are you trying to access?

Share This Page