【问题标题】:Segue to same view controller issue when going back down the navigation stack返回导航堆栈时转到相同的视图控制器问题
【发布时间】:2013-07-02 15:05:42
【问题描述】:

TableViewController 中的单元格由 NSMutableArray 属性填充 - 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

加载 TableViewController 时,会调用一个方法从我的 SQLite 数据库中提取列表。一旦该列表被拉出,它被解析并保存为 NSMutableArray 属性,TableView 数据被重新加载,从而动态填充单元格。

根据选择的单元格确定从 sqlite 数据库中提取的新列表,然后保存到目标控制器的 itemList 属性。

由于导航堆栈下的 segue 数量会有所不同并且取决于选择的单元格,因此我正在通过执行 self segue 并继承新的 NSDictionary 属性来重新加载相同的 TableViewController。

当我继续堆栈时,一切都很好。问题是当我点击后退按钮返回堆栈然后尝试再次返回时。当我将导航堆栈备份到先前加载的视图控制器时,问题似乎是不再可以访问原始属性。有没有其他人遇到过这个问题并且知道解决方案?我猜这是因为我需要让我的 TableViewController 类的一个新实例更动态地向下传递堆栈,而我没有正确执行它。这是我的转场代码:

#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);
}

【问题讨论】:

  • 当你返回堆栈时,你是否只是解雇了 VC?
  • 我只是按了默认的后退按钮。
  • 理论上你可以调用另一个 segue 来返回堆栈,并像你一直在做的那样通过 prepareForSegue 传递你的属性。但它可能会有点马虎:)
  • 你能打电话给[selfdismissModalViewControllerAnimated:YES];从你的后退按钮?
  • 我想如果需要的话我可以,那到底能做什么?

标签: ios ios6 uitableview segue


【解决方案1】:

@property (nonatomic,strong) NSMutableArray *selectedItemList;

我需要添加第二个名为 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

selectedItemList 是在选择单元格时创建的,而不是更改当前 itemList 属性。

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

将该属性传递给新的 TableViewController。

-(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");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    相关资源
    最近更新 更多