static variables accessible to all viewControllers?

Discussion in 'iOS Programming' started by whitedragon101, Jan 11, 2013.

  1. whitedragon101, Jan 11, 2013
    Last edited: Jan 11, 2013

    whitedragon101 macrumors 6502a

    Joined:
    Sep 11, 2008
    #1
    I have just started an app in which i am going to use an SQLlite database. It seems there are two key objects you need to access the database once you have opened it.

    NSString *databasePath;
    sqlite3 *contactDB;

    I open the database on the loading screen but need to have the database accessible to all viewcontrollers in the app. I have tried passing the databasePath and contactDB variables through the prepareForSegue method. This works but seems a little ugly.

    Is there any way to instantiate these two variables as some kind of whole app global variable so I can do something like:
    Code:
    [B]mymainViewController.h
    [/B]
    veryGlobal NSString *databasePath;
    veryGlobal sqllite *contactDB
    
    [B]mymainViewController.m
    [/B]
    - (void)viewDidLoad
    {
     //initialise and open database here
    }
    
    [B]secondViewController.h
    [/B]
    veryGlobal NSString *databasePath;
    veryGlobal sqllite *contactDB
    
    
    [B]secondViewController.m
    [/B]
    -(void)printSomeData
    {
    
        const char *dbpath = [databasePath UTF8String];
       if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {
             //print data from contactDB here
    
        }
    
    }
    
    
    
     
  2. Mvkoe macrumors member

    Mvkoe

    Joined:
    Aug 4, 2008
    Location:
    Belgium
    #2
    Making a "singleton" that handles your data ?
     
  3. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #3
    That would be my suggestion to.
     
  4. whitedragon101 thread starter macrumors 6502a

    Joined:
    Sep 11, 2008

Share This Page