1

I have a tableviewcontroller that needs to drill down and show 3 layers of data. I have no problem drilling down but when I go back, the cells and table become empty. I'm currently using storyboard and I didn't have this problem when I was using nib. All I had to to was alloc and initWithNibName the same view and it would create another instance of the same TableView and I can go back and all the data would be there.

I've tried using segue but it's not working as I need the tableviewcontroller to segue back to itself if I'm drilling down. I've created my own method to push the view controller and pass data into the new instance of itself

- (void)drillDown{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    ExerciseTableViewController *tableView = [storyboard instantiateViewControllerWithIdentifier:@"ExerciseTableView"];
    tableView.title = _detailTitle;
    tableView.delegate = self;
    tableView.level = _level;
    tableView.currentSMID = _currentSMID;
    tableView.currentMID = _currentMID;
    tableView.muscleNameArray = _muscleNameArray;
    if (_level == 2) {
        tableView.submuscleNameArray = _submuscleNameArray;
    }
    [self.navigationController pushViewController:tableView animated:YES];
}

This is what I have in my viewDidLoad

- (void)viewDidLoad {

[super viewDidLoad];



UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addExercise:)];
self.navigationItem.rightBarButtonItem = add;

NSLog(@"level: %i, currentSMID: %i, currentMID: %i", _level, _currentSMID, _currentMID);

if (self.managedObjectContext_ == nil) {
    self.managedObjectContext_ = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}

if (_level == 1) {
    _submuscleNameArray = [self submuscleGroup:_currentMID valueForKeyPath:@"submuscleGroup"];
    _submuscleIDArray = [self submuscleGroup:_currentMID valueForKeyPath:@"submuscleID"];
} else if (_level == 2) {
    //loading and sorting exercise list
    _exerciseList = [self loadExercise:_currentMID sub:_currentSMID];
    _exerciseListSorted = [self sortKeysByName];
} else {
    [self loadMuscleGroup];
}

}

The table view is only populated when I put some of this code in the viewDidAppear but I can't differentiate between drilling down and going back up with the viewDidAppear.

Isn't the navigationController suppose to save my current view and push a new view and when I pop the new view, I go back to my current view?

It seems like everytime I pop the view, the previous view needs to load all over again and this is causing my previous views to become blank.

Any suggestions?

Storyboard attached Storyboard

jon_na_fun
  • 1,113
  • 2
  • 11
  • 19
  • It is supposed to, so I would suggest that in fact you are not popping - you are probably accidentally making a whole new instance of this view controller. - Also keep in mind that `viewDidLoad` is called only once in the life of a view controller, so if you see it being called when you pop, you are not popping. `viewDidAppear`, on the other hand, is called on creation _and_ when you pop. – matt Apr 26 '15 at 00:58
  • "but I can't differentiate between drilling down and going back up with the viewDidAppear" Actually that's not true. If that's what you need to do in order for this to work, you need to ask about that. – matt Apr 26 '15 at 00:58
  • Can you tell me whether I instantiated a new view with the drillDown method above? I've never had this issue with using nib files but storyboard is driving me crazy. – jon_na_fun Apr 26 '15 at 02:14
  • 1
    Let's focus on this line of your question: "but it's not working as I need the tableviewcontroller to segue back to itself if I'm drilling down" What you do is, you have _two_ instances of the same class in the storyboard. You have a push segue from the first to the second, and to get back (pop), if you don't want to just use the built-in Back button, you use an unwind segue. I am worried about how the storyboard is set up now in relation to your code... – matt Apr 26 '15 at 02:33
  • I added a picture of my storyboard. Doesn't navigationviewcontroller automatically take care of the popping? What is the difference between popping and unwind segue? I've tried have 3 instances of the table view (3 layers of drill down) and having them all refer to the same class using segue but that didn't work as well. viewDidLoad is only called once but I suspect that I may be creating a new instance of the view controller or when it's popping, it's removing all my data and causing the previous views to become blank. – jon_na_fun Apr 26 '15 at 05:36
  • Storyboard looks okay as far as that picture can show. Are you saying that `viewDidLoad` is called only once for the Exercises controller? If so, something else must be going on; as you say, it should not be affected by merely pushing over it and popping back to it. – matt Apr 26 '15 at 17:38

1 Answers1

0

So I figured out what was the issue. I have a variable called _level and I use it to track which level of drill down I'm in. When passing this variable to the next level, I've replaced the current level with the next level number therefore when I come back to the previous view. The current level is the same as the next level which screwed things up.

I've solved it by creating a new variable called nextLevel and passing that down instead.

Very basic error on my part.

jon_na_fun
  • 1,113
  • 2
  • 11
  • 19