Global Settings, Preferences.

Discussion in 'iOS Programming' started by mraheel, Jan 23, 2010.

  1. mraheel macrumors regular

    Joined:
    Apr 18, 2009
    #1
    Hi,

    I'm trying to ascertain the best possible way to have a very functional "settings" controller.

    Is there something like "Global Variables"? Something that can be accessed from all ViewControllers.

    Some setting values are BOOLs or Integers.. I can only think of is NSUserDefaults. easy to retrieve and/or set.

    But, For example, a user goes unto the settings tab, which is within my app, and changes a BOOL from YES to NO. and the user comes back to the main view controller.
    And if that BOOL is used in cellForRowAtIndexPath, i'd hav to reload the table to take effects UNLESS we say that app takes effect only after restart, which isnt good.
    Would I hav to access NSUserDefaults in CellForRowAtIndex? to make the change? Any better alternatives?
     
  2. fernandovalente macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #2
    Have you tried this?

    Code:
    -(void)viewWillAppear:(BOOL)animated{
    }
    This executes something when the view is going to appear. You also may try:

    Code:
    #import "myClass.h"
    
    -(void)changeSomething{
    id controller = [[myClass alloc] init];
    [controller executeAnAction];
    }
    NOTE: I didn't try running this to make sure I wrote everything right.

    Let me know if it works.
     
  3. mraheel thread starter macrumors regular

    Joined:
    Apr 18, 2009
    #3
    THat will probably work, I guess i'd still hav to used a variable in any case.
     
  4. fernandovalente macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #4
    If you want to set a variable from other view controller, try this:

    Code:
    #import "myFile.h"
    
    -(IBAction)yourAction:(id)sender{
    id controller = [[myFile alloc]init];
    [controller setValue:@"MyValue" forKey:@"MyVariable"];
    [controller release], controller = nil;
    }
    NOTE: For example you may use setInteger instead of setValue.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Seems awfully temporary. You instantiate an object, assign a value, and then release the object.
     

Share This Page