0

我的根控制器是一个带有静态单元格和导航控制器的 TableViewController。根据选择的单元格,我想推送另一个 ViewController(带有嵌入式 TableView),它将相应地更改其配置(是否启用按钮,来自 db 表或 NSArray 的单元格数据等)。选择一些“控制器 A”单元格将调用“控制器 B”或“控制器 C”并传递一些数据。

我正在尝试的代码如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   switch (indexPath.row) {
        case 0:
        {
            [self performSegueWithIdentifier:@"segueToType" sender:self];
        }
            break;
        case 1:
        {
            [self performSegueWithIdentifier:@"segueToType" sender:self];
        }
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

if ([[segue identifier] isEqualToString:@"segueToType"])
{
    NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    NSInteger rowNumber = selectedIndexPath.row;
    switch (rowNumber) {
        case 0:
        {
            TypeSelectController *selCon = [segue destinationViewController];
            selCon.myPet = self.myPet;
            selCon.sel = @"tipo";
            selCon.delegate = self;
        }
            break;
        case 1:
        {
            TypeSelectController *selCon = [segue destinationViewController];
            selCon.myPet = self.myPet;
            selCon.sel = @"razza";
            selCon.delegate = self;
        }
            break;
        default:
            break;
    }

} 
}

例如,在这种情况下,我只调用“控制器 B”,但根据“控制器 A”的选定单元格,它以不同的方式配置自身(取决于“selCon.sel”字符串)。

在“控制器 B”中,我有这样的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([sel isEqualToString:@"tipo"]) {
    NSString *itemToPassBack = [tipi objectAtIndex:indexPath.row];
    [self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack ofType:@"tipo"];
    [self.navigationController popViewControllerAnimated:YES];
} else if ([sel isEqualToString:@"razza"]) {
    NSString *itemToPassBack = [razze objectAtIndex:indexPath.row];
    [self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack ofType:@"razza"];
    [self.navigationController popViewControllerAnimated:YES];
}
}

同样,根据字符串“sel”值,我将不同的数据返回到“控制器 A”。

当我选择控制器 A 中的第一个单元格时,我正确地获得了控制器 B 场景。然后我选择一个单元格并返回正确数据的控制器 A。现在,如果我尝试在控制器 A 中选择第二个单元格并在控制器 B 中选择一个单元格,则导航不会按我的意愿响应并且我得到控制器 C 场景,然后是控制器 A 和以下错误:

嵌套推送动画可能导致导航栏损坏

在意外状态下完成导航转换。导航栏子视图树可能会损坏。

开始/结束外观转换的不平衡调用。

我究竟做错了什么?

4

2 回答 2

0

在故事板示例中使用协议和委托的快速示例。只是在这里打亮点,因为你已经大部分时间了。

控制器 a(父)。有一个名为 category 的 NSString 属性和一个名为 partNumber 的属性。有一个表视图。选择行来启动一个segue。[self performSegueWithIdentifier:@"segueToType" sender:self];

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"segueToType"]) {
ControllerB *b = segue.destinationViewController; b.category = self.category; b.代表=自我;}

视图控制器 B

@protocol -(void)doSomethingWithPickedPartNumber:(NSString *)partNumber; @结尾

//使用传入的类别的零件号设置表格视图。

//选择行所以我们有我们想要发回的partNumber。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *pn = [self.listOfParts objectAtIndex:indexPath.row];

 //options - set the variable in the parent or call a method in the parent
 self.delegate.partNumber = pn;
 //if you set the variable in the parent - you can check for it in the -(void)viewWillAppear  method and reconfigure the parent view if needed

 //or do this
 [self.delegate doSomethingWithPickedPartNumber:pn];

 //this is actually calling a method in the parent.  remember you don't have a view you can see yet, but its in memory and you can modify things so when the child pops back it will look how you want it.  Also note, you can name the protocol methods what ever you want, as long as they match up from the parent  to the protocol in the child.


 //then return to parent
 [self.navigationController popViewControllerAnimated:YES];

}

于 2012-08-05T17:55:39.943 回答
0

好的,再试一次。

  • 让我们使用头发颜色的例子。您在控制器 a 中有一个表格,其中有一个用于头发颜色的静态单元格。假设您显示所选颜色的文本值。cell.textLabel = self.hairColor。如果它是空白的,用户可以触摸单元格并打开一个新的控制器来选择颜色。

在您选择的新视图控制器中,您设置了一个属性来告诉它查找“hairColor”。因此它从一个名为 listOfHairChoices 的数据库构建一个列表。

用户选择第三行,因此您可以像这样使用委托引用在父级中设置属性。self.delegate.hairColor = [self.listOfHairChoices objectAtIndex:indexPath.row]; 然后弹出关闭子 vc 并弹回父级。[self.navigationController popViewCon..etc]

然后回到父级,在 -(void)viewDidAppear 中,您只需像这样重新加载您的表格视图: [self.tableView reloadData]

现在,您的原始 tableview 应该显示在子视图控制器中选择的单元格的值。

这是一种直接的方法,并且在每个步骤中都需要逻辑来确定您正在使用哪些属性以及需要设置哪些属性 - 但将与您现有的框架一起使用。

另一种方法是创建您正在呈现的数据的模型类,然后将模型传递给子 VC - 让他们更新模型并将其传回,然后只需在每次转换后从更新的模型中重新加载您的表。您可能不熟悉这种方法,但它简化了事情并减少了您现在为 tableview 的每一行所拥有的所有条件逻辑。

希望这个建议对您的项目有所帮助。

于 2012-08-05T18:28:12.943 回答