簡體   English   中英

通過Segue出錯

[英]Passing Segue Gives Error

嗨,我目前已在select上進行了選擇,以將我的表格視圖推送到詳細信息視圖。 我還具有以下代碼來發送所選表格單元的名稱:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *acell = [tableView cellForRowAtIndexPath:indexPath];
    selectedCell = acell.textLabel.text;

    DetailViewController *myDetViewCont = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];

    myDetViewCont.navigationItem.title = selectedCell;
    [self.navigationController pushViewController:myDetViewCont animated:YES];

}

它構建成功,但是在模擬器中拋出錯誤:

@autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
Thread: 1 signal: SIGABRT

我查找了類似的問題,但還沒有找到解決方案。 有幫助嗎?

確定有關該錯誤的其他信息:

[self.navigationController pushViewController:myDetViewCont animated:YES];

如果您使用的是情節提要,則以下幾行沒有意義:

DetailViewController *myDetViewCont = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];

您的項目中是否有一個名為DetailViewController.xib的NIB文件? 大概不是。 因此, myDetViewCont將為nil ,您將獲得一個異常。 如果您確實擁有該NIB,那么之間的推送順序是什么(因為您無法從情節提要到NIB隔離)?

假設您真的要使用情節提要板,而不是NIB,如果您已經在控制器之間建立了推送功能,請為該功能提供一個標識符(您可以在Interface Builder中進行;在下面的代碼中,我將只使用youridentifier作為占位符無論您指定了什么,都將使用正確的標識符替換它),然后應使用segue進行過渡:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *acell = [tableView cellForRowAtIndexPath:indexPath];
    [self performSegueWithIdentifier:@"youridentifier" sender:acell];
}

如果要將數據發送到新控制器,則可以使用prepareForSegue進行此操作:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"youridentifier"])
    {
        if ([sender isKindOfClass:[UITableViewCell class]])
        {
            UITableViewCell *selectedCell = sender;
            UIViewController *myDetViewCont = segue.destinationViewController;
            myDetViewCont.navigationItem.title = selectedCell.textLabel.text;
        }
        else
        {
            NSLog("%s Was expecting sender to be a tableviewcell", __FUNCTION__);
        }
    }
}

找到了解決方案。 您的故事板上需要有一個UINavigationController。 為此,請單擊第一個場景,然后轉到xcode頂部的選項卡,然后執行Editor> Embed in> Navigation controller,選中按鈕以將導航控制器設置為初始場景。 添加以下代碼以將導航欄隱藏在頂部。

self.navigationController.navigationBar.hidden = YES;
self.navigationController.navigationBar.translucent = NO;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM