1290 1290 - 2 months ago 6
iOS Question

String not being passed to next view controller from prepareForSegue

I have a

push segue
on my StoryBoard which is named
toGuestVC
.

I use that to segue to the next
ViewController
in my
didSelectRowAtIndexPath
method like so:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Please not username is a global variable in this ViewController
username = followUsernameArray[indexPath.row]
print("HELLOOOO:\(username)")
performSegue(withIdentifier: "toGuestVC", sender: self)
}


Then in my
prepareForSegue
:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toGuestVC" {
if let nextVC = segue.destination as? GuestCollectionVC, let sender = sender as? CurrentViewController {
print("USER:\(sender.username)")
nextVC.guestUser = sender.username
}
}
}


For some reason this line in my
prepareForSegue
is not running:


nextVC.guestUser = sender.username



When I try to print out the value
guestUser
in my nextViewController the value of
guestUser
is
nil
. But when I print out the value of sender in my
prepareForSegue
method it is not
nil
.

So is my
sender
value not being passed to the next
ViewController
? I can't find a solution to this problem any ideas?

GuestCollectionVC
Implementation:

import UIKit
import Parse

private let reuseIdentifier = "Cell"

class GuestCollectionVC: UICollectionViewController {

var guestUser: String!

override func viewDidLoad() {
loadPosts()
}

func loadPosts() {
//Load posts query
let query = PFQuery(className: PF_POSTS_CLASS)
query.limit = postCount
//Getting error here below this comment when I use guestUser since it is nil
query.whereKey(PF_POSTS_USERNAME_COLUMN, equalTo: guestUser)
query.findObjectsInBackground { (result: [PFObject]?, error: Error?) -> Void in
if error == nil {
if let result = result {

self.uuidOfPosts.removeAll(keepingCapacity: false)
self.imageArrayOfPFFIle.removeAll(keepingCapacity: false)

for postObject in result {
if let uuid = postObject[PF_POSTS_UUID_COLUMN] as? String, let pic = postObject[PF_POSTS_PIC_COLUMN] as? PFFile {
self.uuidOfPosts.append(uuid)
self.imageArrayOfPFFIle.append(pic)
}
}
self.collectionView?.reloadData()
}
}else if error != nil {
print("ERROR FROM GUEST COLLECTION VC FROM loadPosts FUNCTION: \(error?.localizedDescription)")
}
}
}
}


So this is my implementation in the GuestViewController. In my
loadPosts
method where I used the variable
guestUser
I am getting the error:


fatal error: unexpectedly found nil while unwrapping an Optional value

Answer

Since I don't have the full context of the values within your tableView method I can only speculate. That said, the sender you're passing in should be the view controller:

performSegue(withIdentifier: "toGuestVC", sender: username)

You're passing in a value called username which looks to be a string value? It should be something like:

performSegue(withIdentifier: "toGuestVC", sender: self)

where self is your view controller. If you're passing in a string value to sender then in your prepareForSegue method then sender does not have a property called username sender actually is username. Therefore you should pass the value elsewhere:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let indexPath = self.tableView.indexPathForSelectedRow
    if let nextVC = segue.destination as? GuestCollectionVC {
        nextVC.guestUser = followUsernameArray[indexPath.row]
    }
}
Comments