简体   繁体   中英

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. 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.

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.

I embedded a UINavigationControl and made a segue for each 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.

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

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

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. 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. 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

How are you returning from your DetailViewController back to the calling table view controller?

It sounds to me like you are using a normal segue, which is wrong.

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() .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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