If a user clicks on the table-item in my StoriesTableViewController, I need to pass the value of the id to the SingleArtCallViewController so that the SingleArtCallViewController can display the appropriate details.
Thing is, if I print the id of the item that the user has pressed on, it is correctly printing it. But it's not being received in the other ViewController. Can someone solve this problem for me?
StoriesTableViewController:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
finalId = idTable[myIndex]
let story = stories[indexPath.row]
ArtcallID = story.id
performSegue(withIdentifier: "singleArtcall", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(ArtcallID + "is the index of the item that was selected")
//this correctly prints the id
let id = segue.destination as! SingleArtcallViewController
id.ArtcallID = ArtcallID
// print("id: ",id)
}
SingleArtCallViewController:
var ArtcallID: String!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("ArtcallID: ", ArtcallID)
//If I print this I"m getting the result "ArtcallID: some("")"
}
Story.swift:
import Firebase
class Story
{
var id: String = ""
var text: String = ""
var numberOfLikes = 0
var numberOfAngry = 0
let ref: DatabaseReference!
var fundedAmt: Int = 0
var targetAmt: Int = 0
var perfCity: String = ""
var targetDate: IntMax = 0
var backers: Int = 0
var fundedPercent: Int = 0
var imageURL: String = ""
init(text: String) {
self.text = text
ref = Database.database().reference().child("Fund").childByAutoId()
}
init(snapshot: DataSnapshot)
{
ref = snapshot.ref
if let value = snapshot.value as? [String : Any] {
text = value["artist_name"] as! String
backers = value["total_backers"] as! Int
perfCity = value["perf_location"] as! String
fundedAmt = value["amount_funded"] as! Int
targetAmt = value["target_amount"] as! Int
fundedPercent = value["percent_funded"] as! Int
// numberOfLikes = value["numberOfLikes"] as! Int
// numberOfAngry = value["numberOfAngry"] as! Int
id = snapshot.key
}
}
If I understand correctly what you are trying to achieve, and you created the segue by control dragging from prototype cell to SingleArtcallViewController:
The specific reason why code doesn't work is that prepare(for segue:...) runs earlier than tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath). More over you run prepare for segue twice.
If so, You don't need the method
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
Just use prepare(for segue:...):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let selectedRow = tableView.indexPathForSelectedRow?.row,
let vc = segue.destination as? SingleArtcallViewController else {return}
vc.artCallId = stories[selectedRow].id
}