Search code examples
iosxcodestore

Store a ViewController in iOS


I have 3 viewcontrollers that are switched by a swipe gesture (like a carrousel). The data is dynamic, so is there anyway to store the viewcontroller, so when I swipe back to it, it doesn't have to reload the data?


Solution

  • You dont need to store, just dont release them Another way to ensure that you dont release them

    In your app delegate .h file

    Add a property

    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    {
        NSMutableArray *viewControllers;
    }
    @property (retain, nonatomic) NSMutableArray *viewControllers;
    

    In the .m File synthesize it and allocated it in didFinishLaunchingWithOptions

    self.viewControllers = [[NSMutableArray alloc] init];
    

    Now add all your view controllers to this mutable array

    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate.viewControllers addObject:myViewController];
    

    And now when you want to use any of the view controllers you have. you could access it from

    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    myViewContoller = [delegate.viewControllers objectAtIndex:0];