Reputation: 21
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)
}
}
Upvotes: 0
Views: 1634
Reputation: 1375
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
}
}
Upvotes: 0
Reputation: 2160
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
}
}
}
Upvotes: 5