user578386
user578386

Reputation: 1061

viewDidLoad getting called every time

I have a mainView(firstview) where clicking a button takes me to another view(second view) which contains tableview..where I'm parsing data from my server in viewDidLoad .the problem is if I close my second view using dismissmodalView and click the same button in my mainview.the controller goes to the viewDidLoad instead of viewDidAppear..this makes to load the server once again and populate the table view.

-(void)loadFeeds
{
    //Activity Indicator MBProgressHUD
    if(activity == nil){
        activity = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:activity];
        [activity hide:YES];
    }

    //Pull to Refresh (Ego refresh header view) Declarations Facebook and Twitter
    if (_refreshHeaderViewTwitter == nil) {
        EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tblTwitter.bounds.size.height, self.view.frame.size.width, self.tblTwitter.bounds.size.height)];
        view.delegate = self;
        [self.tblTwitter addSubview:view];
        _refreshHeaderViewTwitter = view;
        [_refreshHeaderViewTwitter refreshLastUpdatedDate];
    }

    [activity show:YES];
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

        getTwitterArray = [[NSMutableArray alloc]initWithArray:[Util getJsonArray:Twitter]];

        dispatch_async(dispatch_get_main_queue(), 
                       ^{            
                           [self.tblTwitter reloadData];
                           [activity hide:YES];
                       });
    });
}

Upvotes: 0

Views: 314

Answers (2)

sampage
sampage

Reputation: 420

If you want the view controller to hang around until you dismiss its presenting view controller, then you could retain the view controller as a property

@property (nonatomic, retain) MyModalViewController *myModalViewController;

and lazy load it in it's getter method..

- (MyModalViewController *)myModalViewController
{
  if (_myModalViewController == nil)
  {
    _myModalViewController = [[MyModalViewController alloc] initWithNibName:nil bundle:nil];
  }
  return _myModalViewController;
}

then your view controller will hang around and if/when you get a memory warning, set the property to nil and it will unload correctly.

Upvotes: 0

Pork 'n' Bunny
Pork 'n' Bunny

Reputation: 6731

dismissing the viewcontroller gets rid of it.

You are going to have to manage the persistance manually.

So you are going to need to save the last state of the view controller in some level of persistance higher than the viewcontroller. Wether thats to disk or perhaps even in the appdelegate. It's up to you, but the viewcontroller is working properly.

Upvotes: 1

Related Questions