Search code examples
iosswiftswift4nsdictionary

Passing data using instantiateViewControllerWithIdentifier between ViewControllers


I've been researching this and trying options for the last 2 days and am stumped. I have a page where a user can swipe a cell and sees the following options enter image description here

When the user clicks Report User they should be taken to a page with a form that allows them to submit a complaint about another user. The first VC is called Messages and it should pass the username of both the reporting user and the one being reported. Right now only the reporting user is being passed as it's in a NSDictionary. The latest method I've tried is causing the app to crash with a 'libc++abi.dylib: terminating with uncaught exception of type NSException' Here is a sample of the code:

 let shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Report User" , handler: { (action:UITableViewRowAction!, indexPath:IndexPath!) -> Void in


        let shareMenu = UIAlertController(title: nil, message: "Report User", preferredStyle: .actionSheet)

        let editBtn = UIAlertAction(title: "Harrassment, Inappropriate Content, Abuse", style: .default) { (action:UIAlertAction) in
            self.reportPost(indexPath)

        }
        //let harrassAction = UIAlertAction(title: "Harrassment", style: UIAlertActionStyle.default, handler: nil)
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)

        shareMenu.addAction(editBtn)

        shareMenu.addAction(cancelAction)
       self.present(shareMenu, animated: true, completion: nil)

    })

func reportPost(_ indexPath : IndexPath) {
        let guest = self.guest[indexPath.row]
        let storyboard = UIStoryboard(name: "ReportVC", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "ReportView") as! ReportViewController
        vc.reportLbl.text = guest as? String
    }

Any help is greatly appreciated.


Solution

  • In your ReportVC storyboard (strange name for a storyboard but ok..), select your ReportView scene (in InterfaceBuilder) and verify in the Identity Inspector that its Storyboard ID is set to "ReportView". Also make sure the class is set to ReportViewController.

    It should look like this:

    enter image description here

    Update:

    After chatting with techgirl, it turned out the storyboard wasn't named "ReportVC" after all. So for those reading this answer, please also check that you're referencing the correct storyboard file.