【问题标题】:Segue executed before selecting a cell在选择单元格之前执行 Segue
【发布时间】:2014-10-14 14:04:25
【问题描述】:

我的应用有问题。

我有 3 个视图控制器,我们将它们命名为 OilAppProp。这 3 个视图控制器嵌入在导航控制器中。在那些视图控制器中,我得到了 3 个表格视图。

所以从上到下,从左到右:

Prop View Controller -> Prop Detail View Controller

Oil View Controller -> Oil Detail View Controller-> Web View Controller

App View Controller-> App Detail View Controller

所以在中间 (Oil) 一切正常,我单击一个单元格,然后显示 Oil Detail View Controller,其中包含我通过 Segue 传递的信息。

OilViewController.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sectionTitle = [aromaSectionTitles objectAtIndex:indexPath.section];
    NSArray *sectionAroma = [tableData objectForKey:sectionTitle];
    aromaNom = [sectionAroma objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"Detail" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"Detail"]) {
        OilDetailViewController *test = segue.destinationViewController;
        test.aromaName = aromaNom;
        test.botaniqueName.text = botaniqueName;
    }
}

我也在 Oil Detail View ControllerWeb View Controller 之间执行 Segue,如下所示:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"webView"]) {
        NSLog(@"Segue is catched");
        WebViewViewController *webView = segue.destinationViewController;
        webView.aromaUrl = _aromaName;
    }
}

我点击橙色按钮,它会显示 web 视图。

但是现在如果我在PropApp 中做同样的事情,它就不能正常工作。

例如在Prop Detail VC 中,我得到了一个显示所选属性的标签,但在显示标签中选择的正确属性之前,它向我显示了带有包含旧选定属性的标签的视图控制器。所以我有 2 个 Prop Detail View 显示并嵌入到我的导航控制器中。

我制作了模拟器的视频http://videobam.com/VwgLx

PropViewController.m中的代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sectionTitle = [aromaPropSectionTitles objectAtIndex:indexPath.section];
    NSArray *sectionProp = [propData objectForKey:sectionTitle];
    propName = [sectionProp objectAtIndex:indexPath.row];
    NSLog(@"prop name = %@", propName);
    [self performSegueWithIdentifier:@"PropDetail" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"PropDetail"]) {
        PropDetailViewController *propView = segue.destinationViewController;
        propView.propStr = propName;
        NSLog(@"PREPARE FOR SEGUE PROP VIEW CONTROLLER");
    }
}

PropDetailViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.propLabel.text = _propStr;
    NSLog(@"VIEW DID LOAD PROP DETAIL");
    // Do any additional setup after loading the view.
}

最后是日志:

在此先感谢您的帮助,我被困住了,我的团队中没有人知道 iOs .. 所以我一个人处理这个!

【问题讨论】:

  • 天哪!你的故事板是一个网络 :) 你能做简单的安排吗(并排)。我几乎看不到实际的流量。
  • 忘记左边的网页吧,它只是三个按钮和相应视图之间的链接.. 没那么重要,重要的链接是可见的

标签: ios objective-c iphone segue uistoryboardsegue


【解决方案1】:

所以我终于找到了一个合适的解决方案..我仍然不知道为什么它可以在Oil 上工作并且会导致其他视图出现问题但仍然;这是解决方案:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sectionTitle = [aromaPropSectionTitles objectAtIndex:indexPath.section];
    NSArray *sectionProp = [propData objectForKey:sectionTitle];
    propName = [sectionProp objectAtIndex:indexPath.row];
    NSLog(@"prop name = %@", propName);
    [self performSegueWithIdentifier:@"PropDetail" sender:self];
}

删除上面的方法。并在 prepareForSegue 方法中执行您在此方法中所做的所有事情:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"PropDetail"]) {
        PropDetailViewController *propView = segue.destinationViewController;
        NSIndexPath *indexPath = [self.propTableView indexPathForSelectedRow];
        NSString *sectionTitle = [aromaPropSectionTitles objectAtIndex:indexPath.section];
        NSArray *sectionApp = [propData objectForKey:sectionTitle];
        NSString *name = [sectionApp objectAtIndex:indexPath.row];
        propView.propStr = name;
    }
}

现在它工作正常!但它在一个地方有效,在另一个地方无效,这不是逻辑..

【讨论】:

    猜你喜欢
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多