Passing object with prepareForSegue Swift

好久不见. 提交于 2019-11-27 14:37:00

问题


I am trying to pass an object to another scene with prepareForSegue()

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        var nextScene =  segue.destinationViewController as! VehicleDetailsTableViewController

        // Pass the selected object to the new view controller.
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let selectedVehicle = vehicles[indexPath.row]
            nextScene.currentVehicle = selectedVehicle
        }
    }

And I have currentVehicle object to catch these object. But, when I try to run, it brokes and get error about downcasting.

Error EDIT

Could not cast value of type 'XXX.DisplayViewController' (0x1082dcd80) to 'XXX.VehicleDetailsTableViewController' (0x1082dc9a0). (lldb)


回答1:


You have to give the segue an identifier in the storyboard.(say mySegue)

Using Xcode 10 swift 4.x(Also works with Xcode 9 & 8 , swift 3.x)

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

Is called for all segues being called from your current UIViewController. So the identifier is to differentiate the different segues

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "mySegue" , 
       let nextScene = segue.destination as? VehicleDetailsTableViewController , 
       let indexPath = self.tableView.indexPathForSelectedRow {
        let selectedVehicle = vehicles[indexPath.row]
        nextScene.currentVehicle = selectedVehicle
    }
}

If you are using Using Xcode 7, swift 2.x

Then use this code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "mySegue" {
    var nextScene =  segue.destinationViewController as! VehicleDetailsTableViewController

    // Pass the selected object to the new view controller.
    if let indexPath = self.tableView.indexPathForSelectedRow {
        let selectedVehicle = vehicles[indexPath.row]
        nextScene.currentVehicle = selectedVehicle
    }
  }
}

Place a breakpoint after nextScene and see if it is being triggered by clicking any cell in the TableView. If it isn't then the identifier name u provided in the storyboard must be different then the one given here.




回答2:


In swift 3.0 Xcode 8 beta 6

AnyObject has been renamed to Any?

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    let nextScene =  segue.destinationViewController as! VehicleDetailsTableViewController

    // Pass the selected object to the new view controller.
    if let indexPath = self.tableView.indexPathForSelectedRow {
        let selectedVehicle = vehicles[indexPath.row]
        nextScene.currentVehicle = selectedVehicle
    }
  }
}



回答3:


In Swift 3, Xcode 8

Just don't use "identifier". I don't know why but "segue.identifier" is always nil. I just used prepare method like this and works!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let secondController = segue.destination as? SecondController {
        secondController.anyItemInSecondController = self.anyItemInFirstController
    }



回答4:


What that error is telling you is that segue.destinationViewController isn't a VehicleDetailsTableViewController. We don't have enough details to tell you why.

Check your segues and make sure they're all pointing to the correct place, and always check the identifier of your segue before you perform a cast.

if segue.identifier == "theNameOfYourSegue" // then do your cast


来源:https://stackoverflow.com/questions/31164654/passing-object-with-prepareforsegue-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!