Preparing for segue in embedded tableView in Swift

落爺英雄遲暮 提交于 2019-12-08 05:47:43

问题


I have a UITableViewController with static cells embedded in a normal ViewController through a storyboard. This is al working as expected. However when I want to prepare for a segue when a cell on the embedded tableview is clicked the app crashes without a real error.

I have a class for the embedded UITableViewController, TableViewVC, and a class in which it's embedded, RootVC. I want to implement the method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?);

I created the segue in my storyboard from a specific static cell in my TableViewVC. When I implement this method in TableViewVC it never gets called. It does get called in RootVC however but crashes when I try to access the segue.identifier property. When I debug the app here I can see that the type is of the segue is:

UIStoryboardEmbedSegue

Does anyone know how I can prepare for this segue properly and access the identifier property to know what cell is clicked?

Edit: screenshot of the error: http://cl.ly/image/2z031b2B3i0o


回答1:


The UIStoryboardEmbedSegue is used for embed relationships in ViewControllers. It is setting up the parent-child relationship between your RootVC and TableVC. It crashes because chances are you didn't se the identifier for this particular segue. You shouldn't be overriding this function in the RootVC if you are not interested in the embed relationship.

It looks like you didn't set up the segue from your cells properly. If you post a image of your storyboard, we can take a look.

An alternative to this is to set up a segue from your TableVC (not the cells), by control dragging from the yellow VC icon on top of the VC in the storyboard to the destination you want. And then performing the segue programmatically in didSelectRow.




回答2:


I had a similar problem. You cannot access segue.identifier if the identifier is left empty in storyboard.



来源:https://stackoverflow.com/questions/26113910/preparing-for-segue-in-embedded-tableview-in-swift

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