简体   繁体   English

Swift3和Segue:两个不同的UITableViewController指向一个UIView

[英]Swift3 and Segue: Two different UITableViewController point to one UIView

I have an App in Swift3 that consists of 2 different UITableView that show details of a Class Asset. 我在Swift3中有一个包含2个不同的UITableView的应用程序,其中显示了类资产的详细信息。 The content of the data in both table is same/similar (there are some filters), the presentation is different. 两个表中的数据内容相同/相似(有一些过滤器),表示形式也不同。 So one of this UIViewTables shows for example comments for each asset and the other is more focused on the status. 因此,其中一个UIViewTables例如显示每个资产的注释,而另一个则更关注状态。

If a user clicks on a table cell, both UITableViews open the same UIView for full details since both tables represent the same data at the end. 如果用户单击表单元格,则两个UITableViews都将打开同一UIView以获得完整的详细信息,因为两个表末尾都表示相同的数据。

I embedded a UINavigationControl and made a segue for each TableCell. 我嵌入了一个UINavigationControl并为每个TableCell进行了排序。 If I click on the cells, the correct detail screen opens. 如果单击单元格,则会打开正确的详细信息屏幕。

But if I save it, it always brings me back to the first table, even if I start from the second. 但是,如果我保存了它,即使我从第二个表开始,它也总是使我回到第一个表。

Can anybody give me a hint? 有人可以给我提示吗?

Here is my DetailViewController prepare method that is called when I click on the save button. 这是我单击保存按钮时调用的DetailViewController prepare方法。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    super.prepare(for: segue, sender: sender)

    print(segue.identifier)

    // Configure the destination view controller only when the save button is pressed.
    guard let button = sender as? UIBarButtonItem, button === btnSave else
    {
        //os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug)
        return
    }

    asset?.name = tfDeviceName.text ?? ""
    //here comes some other stored asset information...
}

Here is my first UITableViewController 这是我的第一个UITableViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    super.prepare(for: segue, sender: sender)

    switch(segue.identifier ?? "")
    {

    case "AddItem":
        os_log("Adding a new asset.", log: OSLog.default, type: .debug)

    case "ShowDetails":
        guard let assetDetailViewController = segue.destination as? DetailViewController else {
            fatalError("Unexpected destination: \(segue.destination)")
        }

        guard let selectedAssetCell = sender as? AssetTableViewCell else {
            fatalError("Unexpected sender: \(String(describing: sender))")
        }

        guard let indexPath = tableView.indexPath(for: selectedAssetCell) else {
            fatalError("The selected cell is not being displayed by the table")
        }

        selectedIndex = indexPath.row

        let selectedAsset = assets[indexPath.row]
        assetDetailViewController.asset = selectedAsset

    default:
        fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
    }
    editMode = true
}

This is my second UITableViewController 这是我的第二个UITableViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    super.prepare(for: segue, sender: sender)

    switch(segue.identifier ?? "") {

    case "AddItem":
        os_log("Adding a new asset.", log: OSLog.default, type: .debug)

    case "ShowTask":
        guard let taskDetailViewController = segue.destination as? DetailViewController else {
            fatalError("Unexpected destination: \(segue.destination)")
        }

        guard let selectedAssetCell = sender as? TaskTableViewCell else {
            fatalError("Unexpected sender: \(String(describing: sender))")
        }

        guard let indexPath = tableView.indexPath(for: selectedAssetCell) else {
            fatalError("The selected cell is not being displayed by the table")
        }

        let selectedAsset = assets[indexPath.row]
        taskDetailViewController.asset = selectedAsset

    default:
        fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
    }
}

And here my Story Board 这是我的故事板 在此处输入图片说明

Thank you! 谢谢! BR Stefan 斯特凡

EDIT WITH SOLUTION: 使用解决方案进行编辑:

Thx to Duncans hints I found my problem. 感谢邓肯(Tuncans)暗示我发现了我的问题。 The unwind action is attached to the save button in the interface builder by crtl-click-drag the button to the exit event and select then the unwind method. 通过crtl-click-拖动按钮到退出事件,然后选择unwind方法,将unwind操作附加到界面构建器中的save按钮。 And here comes the important thing: You can select only one method, but not the class. 重要的是:您只能选择一种方法,而不能选择类。 The class is selected automatically by the system that manages the navigation, but both methods (obviously) have to have the same unwind name. 该类由管理导航的系统自动选择,但两种方法(显然)必须具有相同的展开名称。 I made a typo so that the unwind method in the second UITableView was slightly different and then the systems doesn't find the method in the correct UITableView and jumps to a UITableView that has the correct method even is this Class was not the original segue 我做了一个错字,以便第二个UITableView中的unwind方法略有不同,然后系统在正确的UITableView中找不到该方法,并跳转到具有正确方法的UITableView,即使该Class不是最初的segue

How are you returning from your DetailViewController back to the calling table view controller? 您如何从DetailViewController返回到调用表视图控制器?

It sounds to me like you are using a normal segue, which is wrong. 在我看来,您使用的是正常的Segue,这是错误的。

You can either use an unwind segue, or the reverse of whatever method you use to get there (if you use a push segue, call pop() , if you use a modal present, call dismiss() .) 您可以使用展开顺序,也可以使用相反的方法来达到此目的(如果使用强制顺序,则调用pop() ,如果使用模式存在,则调用dismiss() 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM