NSGodMode
NSGodMode

Reputation: 627

Call method of Existing Instance of AppController from AppDelegate

I have a class called AppController, And I have following method in AppDelegate which is invoked when menu item is selected.

-(IBAction)selectSug:(id) sender
{

    AppController * vc = [[AppController alloc]init];

    [vc selectSugItem:sender];

}

But this method creates a new instance of AppController so I cannot use existing values of the variables in the AppController.

Please help me with a solution.Thanks

Upvotes: 0

Views: 234

Answers (1)

Dima
Dima

Reputation: 23634

If this AppController class is meant to be a singleton (meaning there should only be one instance of it that the rest of the application will access), you can simply create one.

Add this method to your AppController class (and add the header declaration):

+ (instancetype)shared
{
    static id shared = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        shared = [[self alloc] init];
    });
    return shared;
}

Then you can call it by doing this:

-(IBAction)selectSug:(id) sender
{
    AppController * vc = [AppController shared];

    [vc selectSugItem:sender];
}

Upvotes: 1

Related Questions