Objective C: store variables accessible in all views

前端 未结 2 1233
失恋的感觉
失恋的感觉 2020-12-18 17:12

Greetings,

I\'m trying to write my first iPhone app. I have the need to be able to access data in all views. The data is stored when the user logs in and needs to be

相关标签:
2条回答
  • 2020-12-18 17:34

    Use a singleton class, I use them all the time for global data manager classes that need to be accessible from anywhere inside the application. You can create a simple one like this:

    @interface NewsArchiveManager : NetworkDataManager
    {
    }
    
    + (NewsArchiveManager *) sharedInstance;
    @end
    
    @implementation NewsArchiveManager
    
    - (id) init
    {
        self = [super init];
        if ( self ) 
        {
             // custom initialization goes here
        }
    
        return self;
    }
    
    
    + (NewsArchiveManager *) sharedInstance
    {
        static NewsArchiveManager *g_instance = nil;
    
        if ( g_instance == nil )
        {
            g_instance = [[self alloc] init];
        }
    
        return g_instance;
    }
    
    
    - (void) dealloc
    {
        [super dealloc];
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-18 17:42

    I don't know what you mean with "static class", but what you want is a singleton. See this question for various methods on how to set one up.

    0 讨论(0)
提交回复
热议问题