从 TableViewController 到嵌入在 NavController 中的第二个 TVC 的 performSegueWithIdentifier 不起作用

Posted

技术标签:

【中文标题】从 TableViewController 到嵌入在 NavController 中的第二个 TVC 的 performSegueWithIdentifier 不起作用【英文标题】:performSegueWithIdentifier from TableViewController to second TVC embedded in NavController not working 【发布时间】:2011-12-23 21:07:59 【问题描述】:

我有两个 TableViewControllers,中间有一个 segue。当用户点击第一个 TVC 中的单元格时,他们会看到第二个 TVC。 segue 是模态的,有一个名为“segueToLocationDetails”的标识符,并与它一起传递一个对象。您可以或多或少地将第二个 TVC 视为“详细信息”页面。

我的代码在我上面描述的场景中完美运行。但是,一旦我将第二个 TVC 嵌入导航控制器,它就会中断。

示例。我让它完美地工作。然后我突出显示 IB 中的第二个 TVC,将鼠标悬停到 Product |嵌入 |导航控制器。现在第二个 TVC 在导航控制器中。然而,segue 仍然指向第二个 TVC。我删除了 segue 并将其从第一个 TVC 的单元格重新连接到导航控制器,并确保为 segue 提供一个标识符。再跑,它就坏了!错误如下...

2011-12-23 15:30:45.469 Project12[5219:11603]-[UINavigationController setDetailsObject:]: 无法识别的选择器发送到实例 0x7b92ce0 2011-12-23 15:30:45.471 Project12[5219:11603] * 终止应用程序到期 未捕获的异常“NSInvalidArgumentException”,原因: '-[UINavigationController setDetailsObject:]: 无法识别的选择器 发送到实例 0x7b92ce0' * 第一次抛出调用栈:(0x16ea052 0x150ad0a 0x16ebced 0x1650f00 0x1650ce2 0x3933 0x703e1e 0x36f6d9 0x36f952 0xbf786d 0x16be966 0x16be407 0x16217c0 0x1620db4 0x1620ccb 0x14ec879 0x14ec93e 0x2dfa9b 0x2a98 0x29f5 0x1) 终止调用抛出异常当前 语言:自动;目前客观-c

下面是一些代码来帮助解释:

AllLocations.h & AllLocations.m(这是主表)

AllLocations.h

@interface AllLocations : UITableViewController

    SQLiteDB *mysqliteDB;

@property (nonatomic, strong) NSMutableArray *locationsArray;



AllLocations.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    [self performSegueWithIdentifier:@"segueToLocationDetails" sender:self];


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    if ([[segue identifier] isEqualToString:@"segueToLocationDetails"]) 
    
        NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
        NSInteger rowNumber = selectedIndexPath.row;

        mySQLiteDB = (SQLiteDB *) [locationsArray objectAtIndex:rowNumber];

        DetailsTVC *detailsTVC = [segue destinationViewController];

        detailsTVC.detailsObject = mySQLiteDB;        
    

DetailsTVC.h & DetailsTVC.m(这是详细的表格视图)

DetailsTVC.h

@interface DetailsTVC : UITableViewController

@property (nonatomic, strong) SQLiteDB *detailsObject;


DetailsTVC.m

@implementation SpotDetailsTVC

@synthesize spotDetailsObject;

注意:我省略了所有与问题无关的代码。

再次重申:如果 segue 从 Originating TableVeiwController 转到另一个 TableViewController,这将非常有效。只有当我将第二个 TVC 嵌入到导航控制器中时,它才会中断。我需要知道如何使用图片中的导航控制器来实现它。提前致谢!

【问题讨论】:

【参考方案1】:

DetailsTVC *detailsTVC = [segue destinationViewController];

那行不正确。由于您的第二个 TVC 现在嵌入在导航控制器中,因此 [segue destinationViewController] 现在是 UINavigationController。这应该有效:

DetailsTVC *detailsTVC = [[segue destinationViewController] visibleViewController];

【讨论】:

好的,谢谢,但是我现在收到此警告:file://localhost/Users/me/Desktop/Project13/AllLocations.m:警告:语义问题:不兼容的指针类型正在初始化' locationDetailsTVC *__strong' 类型为“UIViewController *”的表达式 prepareForSegue:sender: 中试试这个以找出destionationController 是什么:NSLog(@"%@", NSStringFromClass([[segue destinationViewController] class])) 2011-12-23 16:32:20.741 Project13[5452:11603] UINavigationController 也许您需要转换该变量,因为发件人是id。不太确定该警告的含义。 这也适用于我,但现在我得到了这个异常:*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“不支持推送导航控制器”。有什么办法可以解决这个问题?

以上是关于从 TableViewController 到嵌入在 NavController 中的第二个 TVC 的 performSegueWithIdentifier 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

从父 ViewController 更改嵌入 TableViewController 的数据

IOS:来自tableviewcontroller的UITabBarControllerDelegate方法

如何在 TableViewController 中实现后退导航按钮?

从静态单元到动态 TableViewController

是否可以从嵌入式 UITableViewController 设置父 UITabViewController 属性

Swift:如何将数据从 TableViewController 传递到 Details VC