0

I need to pass this string value to another viewController. For some reason I am getting a sigabrt error. Can anyone point out what im doing wrong?

Need to pass userIdentityString value to userCellTapped viewcontroller

    class GeneralChatroom: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UITextViewDelegate {


//Get Data of current cell that has been tapped
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

            //Eliminate highlight after cell is tapped
            tableView.deselectRow(at: indexPath as IndexPath, animated: true)

            let userIdentityString : String = generalRoomDataArr[indexPath.row].cellUserId

            let destinationUID = userCellTapped()

            destinationUID.programVar = userIdentityString

            destinationUID.performSegue(withIdentifier: "profileTapped", sender: self)


        }

}


    import UIKit

    class userCellTapped: UIViewController {

        var programVar : String!


        override func viewDidLoad() {
            super.viewDidLoad()


            print("testbles", programVar)


        }


    }
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – pableiros Apr 25 '17 at 22:54

2 Answers2

5

The way to properly set destination view controller variables is by implementing prepare(for segue ...) in your ViewController class:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "profileTapped" {
        if let destination = segue.destination as? MyDestinationViewControllerType {
            destination.myVariable = myClassLevelVariable
        }
    }
}
Forest Kunecke
  • 2,160
  • 15
  • 32
0

In order to call method

destinationUID.performSegue(withIdentifier: "profileTapped", sender: userIdentityString)

you have to add Segue with identifier "profileTapped" into the storyboard. Sender in the method is the value you want to pass. As identifier you should pass id of that Segue.

And if you want to pass some data to that controller. Yoy have to implement additional method

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "profileTapped", let vc = segue.destination as? UserCellTapped, let variable = sender as? String {
        vc.programVar = variable
    }
}
Sander
  • 1,325
  • 1
  • 11
  • 30