Search code examples
iosios6uitableviewsegue

Segue to same view controller issue when going back down the navigation stack


The cells in my TableViewController are populated from an NSMutableArray property - itemList.

@interface MenuViewController : UITableViewController <JSONURLConnectionDelegate>

@property (nonatomic,strong) JSONURLConnection *jsonConnection;
@property (nonatomic,strong) NSURL *jsonURL;   
@property (nonatomic,strong) DataBase *db;
@property (nonatomic,strong) NSString *sel_category;
@property int categoryCount;   
@property (nonatomic,strong) NSMutableArray *itemList;    

@end

When the TableViewController is loaded, a method is called to pull a list from my SQLite database. Once that list is pulled, it's parsed and saved as the NSMutableArray property, the TableView data is reloaded, thus populating the cells dynamically.

Depending on which cell is selected determines what new list is pulled from the sqlite database and then saved to the destination controller's itemList property.

Since the number of segues down the Navigation stack vary and are dependent on what cells are selected, I am reloading the same TableViewController by doing a self segue and carrying over the new NSDictionary property.

enter image description here

Everything works great when I segue down the stack. The problem is when I hit the back button to go back up the stack and then try to go back down again. The issue seems to be with the original properties no longer being accessible when I go back up the navigation stack to a previously loaded view controller. Has anyone else had this problem and know of a solution? I am guessing it's because I need to have a new instance of my TableViewController class passed down the stack more dynamically and I'm not doing it correctly. Here is my segue code:

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    NSDictionary *item = [self.itemList objectAtIndex:indexPath.row];

    NSData *jsonData = [[item objectForKey:@"child_categories"] dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *child_categories = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

    self.categoryCount  = [child_categories count];
    self.sel_category   = [item objectForKey:@"id"];


    NSString *sql = [[NSString alloc]initWithFormat:@"WHERE parent_category_id = %@ ORDER BY name ASC", _sel_category];
    self.itemList = [self.db getItemsFrom:@"product_category" where:sql];

    if ([child_categories count] > 0)
    {
        [self performSegueWithIdentifier:@"segue1" sender:self];
    }
    else if([child_categories count] == 0)
    {
        [self performSegueWithIdentifier:@"segue2" sender:self];
    }
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"segue1"])
    {
        MenuViewController *mvc = [segue destinationViewController];
        mvc.jsonConnection  = self.jsonConnection;
        mvc.db              = self.db;
        mvc.sel_category    = self.sel_category;
        mvc.categoryCount   = self.categoryCount;
        mvc.itemList        = self.itemList;
        NSLog(@"Segue1");

    }
    else if([segue.identifier isEqualToString:@"segue2"])
    {
        NSLog(@"Segue2");
    }
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    //ignore segue from cell since we we are calling manually in didSelectRowAtIndexPath
    return (sender == self);
}

Solution

  • @property (nonatomic,strong) NSMutableArray *selectedItemList;

    I needed to add a second property called selectedItemList.

    @interface MenuViewController : UITableViewController <JSONURLConnectionDelegate>
    
    @property (nonatomic,strong) JSONURLConnection *jsonConnection;
    @property (nonatomic,strong) NSURL *jsonURL;   
    @property (nonatomic,strong) DataBase *db;
    @property (nonatomic,strong) NSString *sel_category;
    @property int categoryCount;   
    @property (nonatomic,strong) NSMutableArray *itemList;
    @property (nonatomic,strong) NSMutableArray *selectedItemList;       
    
    @end
    

    The selectedItemList is created when a cell is selected instead of changing the current itemList property.

    NSString *sql = [[NSString alloc]initWithFormat:@"WHERE parent_category_id = %@ ORDER BY name ASC", _sel_category];
    self.selectedItemList = [self.db getItemsFrom:@"product_category" where:sql];
    

    Pass that property to the new TableViewController instead.

    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if([segue.identifier isEqualToString:@"segue1"])
        {
            MenuViewController *mvc = [segue destinationViewController];
            mvc.jsonConnection  = self.jsonConnection;
            mvc.db              = self.db;
            mvc.sel_category    = self.sel_category;
            mvc.categoryCount   = self.categoryCount;
            mvc.itemList        = self.selectedItemList;
            NSLog(@"Segue1");
    
        }
        else if([segue.identifier isEqualToString:@"segue2"])
        {
            NSLog(@"Segue2");
        }
    }