0

I have a parent view controller with this code:

@IBOutlet weak var artist1: UILabel!

@IBAction func unwindFromArtist(segue: UIStoryboardSegue) {
    //add artist
    let source = segue.source as? ViewControllerArtistSearch
    //Getting new artist and setting it to the appropriate artist label
    artist1.text = (source?.favoriteArtist)!
}

And I am presenting a table view controller modally with this code:

import UIKit
import Alamofire

class ViewControllerArtistSearch: UITableViewController, UISearchBarDelegate {

var names = [String]()
var favoriteArtist: String = "fuckdickdonteattidepods"

@IBOutlet weak var artistSearch: UISearchBar!

//What happens with search is clicked
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    let keywords = searchBar.text
    let searchkeywords = keywords?.replacingOccurrences(of: " ", with: "+")

    searchURL = "https://itunes.apple.com/search?term=\(String(describing: searchkeywords!))&entity=musicArtist"

    callAlamo(url: searchURL)
}

var searchURL: String = ""

override func viewDidLoad() {
    super.viewDidLoad()
}

func callAlamo(url: String){
    Alamofire.request(url).responseJSON(completionHandler: {
        response in

        self.parseData(JSONData: response.data!)
    })
}

//Going through the json response from itunes and parsing out only the artist name
func parseData(JSONData: Data) {
    do {
        let readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as? [String : AnyObject]
        if let results = readableJSON!["results"] as? NSArray{
            for i in 0..<results.count{
                let artist = results[i] as? NSDictionary
                let artistName = artist?["artistName"]
                names.append((artistName as? String ?? "artist"))
                self.tableView.reloadData()
            }
        }
    }
    catch {
        print(error)
    }
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return names.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
    cell?.textLabel?.text = names[indexPath.row]
    return cell!
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    favoriteArtist = (names[indexPath.row])
    print(favoriteArtist)
}

I have it set so that when the user selects a cell from the table view an unwind segue is performed to move back to the parent vc.

However I also have it set so that the code grabs the value from the row with didSelectRowAt and sets it to an empty string to be passed back to the parent.

The problem is that the segue is performed before the value is pulled from the row and set to the variable, so the value passed back to the parent is the default "fuckdickdonteattidepods".

I am totally stuck at this issue, any advice would be greatly appreciated!

Levi Weiss
  • 13
  • 3

1 Answers1

0

Well, the first thing that comes to my mind is to remove the unwind segue, create a manual unwind segue (see this answer) with unwindSegueIdentifier and then call it programmatically in didSelectRowAt:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    favoriteArtist = (names[indexPath.row])
    print(favoriteArtist)
    self.performSegue(withIdentifier: "unwindSegueIdentifier", sender: self)
}
Milan Nosáľ
  • 17,130
  • 3
  • 42
  • 73