iOS Objective-C:如何从另一个类调用Push Segue



场景

我有一个应用程序,它在UITableViewController中向用户显示帖子,在某些包含评论的帖子中,该单元格中会有另一个UITableView来显示评论。每个评论都将包含一个位于UIButton下方的个人资料图片。当按下这个UIButton时,我希望UITableViewController类转到另一个UIViewController,它将显示关于发布帖子的用户的信息。

因此,回顾一下

UITableViewController将执行Push Segue

UITableViewController具有UITableViewCells

UITableViewCells(包含注释)将有一个子UITableView

UITableView将包含细胞本身,每个细胞都有一个UIButton

UIButton被推送时,UITableViewController将执行推送分段

我尝试了什么

- (void)ProfilePictureWasPushed:(UIButton *)sender {
    NSDictionary *object = [self.comments objectAtIndex:sender.tag];
    DashboardViewController *dashboard = [[DashboardViewController alloc]init];
    dashboard.selectedID = object[@"posterID"];
    [dashboard performSegueWithIdentifier:@"profile" sender:dashboard];
}

我收到这个错误。。。

...reason: 'Receiver (<DashboardViewController: 0x7d867600>) has no segue with identifier 'profile''

我已经检查过了,事实上有一个推送片段,叫做"个人资料"。

问题

有人知道如何正确地完成这一壮举吗?

根据此处的文档,启动和接收片段的UIViewControllers必须使用情节提要加载https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instm/UIViewController/performSegueWithIdentifier:sender:

接收此消息的视图控制器必须已从情节提要加载。如果视图控制器没有关联的情节提要,可能是因为您自己分配并初始化了它,则此方法会引发异常

如果要从ViewControllerA分段到ViewControllerB,则需要在ViewControllerA的实例上调用该方法。此外,您不应该创建ViewControllerB。所以你的代码应该是

- (void)ProfilePictureWasPushed:(UIButton *)sender {
    [self performSegueWithIdentifier:@"profile" sender:sender]; //self could also be used for sender
}

由于您还想通过segue将数据传递到目标视图控制器,因此要执行此操作,请覆盖源视图控制器(即您的UITableViewController)中的-prepareForSegue:sender,检索destinationViewController并设置selectedID。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([[segue identifier] isEqualToString:@"profile"]]) {
         NSDictionary *object = [self.comments objectAtIndex:sender.tag];
         DashboardViewController *dashboard = (DashboardViewController *)segue.destinationViewController;
         dashboard.selectedID = object[@"posterID"];
      }
}

"profile"segue标识符是否位于DashboardViewController上??请在项目的Main.storyboard上查看。

示例:"海报"segue关于这个项目位于MyViewController场景

字符串"profile"应在情节提要中显式分配。在界面生成器中选择segue,然后转到属性检查器,检查Indetifier是否已定义并拼写正确。

最新更新