Instance method returns correct array from one view controller but null from others

56 Views Asked by At

This is my first post so apologies for any errors I make, but this has been frustrating me for hours now and I cannot find a solution.

I have an app that uses a UITabBarViewController that has 3 tabs: FirstViewController, SecondViewController and ThirdViewController.

I have an NSObject class (Manager) where I reach out to from my view controllers to pull information from the calendar. Which works perfectly fine when I use the FirstViewController, however, when I go to use the other view controllers it simply returns "null" but I know the instance method is being called because I put an NSLog in the instance method it returns a value, but this value isn't being passed onto view controllers two and three.

The code I am using is below.

AppDelegate.m

    #import <UIKit/UIKit.h>
    #import "EventManager.h"

    @interface AppDelegate : UIResponder <UIApplicationDelegate>{

    }

    @property (strong, nonatomic) UIWindow *window;
    @property (nonatomic, strong) EventManager *eventManager;

    @end

AppDelegate.m

    - (void)applicationDidBecomeActive:(UIApplication *)application {

        self.eventManager = [[EventManager alloc] init];

    }

EventManager.h

    #import <Foundation/Foundation.h>
    #import <EventKit/EKEventStore.h>

    @interface EventManager : NSObject

    @property (nonatomic, strong) EKEventStore *eventStore;

    -(NSMutableArray *) fetchCalendars;
    @end

EventManager.m

    -(NSMutableArray *) fetchCalendars {

        NSArray *EKCalendars = [self.eventStore         calendarsForEntityType:EKEntityTypeEvent];

        NSMutableArray *mutableCalendars = [[NSMutableArray alloc]         initWithArray:EKCalendars];
        NSLog(@"EKCalendars %@",EKCalendars);

        return mutableCalendars;
    }

FirstViewController.m

    -(void)loadCalendars{


            NSMutableArray *mutableCalendars = [self.appDelegate.eventManager fetchCalendars];

    }

This works absolutely fine for loading the calendars.

SecondViewController.m

    -(void)loadCalendars{


            NSMutableArray *mutableCalendars = [self.appDelegate.eventManager fetchCalendars];

    }

This returns null, however, the output from the NSLog[ NSLog(@"EKCalendars %@",EKCalendars)] gives the exact same output as when it code is ran for the First View Controller.

I can get the calendars from the altering the SecondViewController.m to read

    -(void)loadCalendars{


    EventManager *per= [[EventManager alloc]init];
    calendarsArray =   [per fetchCalendars];

    }

But I just don't understand why I need to reinitialize the event manager as it is initialized in the applicationDidBecomeActive.

Thanks for any help you guy can give.

1

There are 1 best solutions below

1
On BEST ANSWER

You can access the Application's AppDelegate using [UIApplication sharedApplication].delegate:

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSMutableArray *mutableCalendars = [appDelegate.eventManager fetchCalendars];