Modal View Controller 中的 Unwind Segue 发生得太早

Unwind Segue Happening Too Early in Modal View Controller

我有一个父视图控制器,代码如下:

@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)!
}

我正在使用以下代码以模态方式呈现 table 视图控制器:

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)
}

我已将其设置为当用户从 table 视图中选择一个单元格时执行展开转场以返回父级 vc。

但是我也设置了它,以便代码从带有 didSelectRowAt 的行中获取值并将其设置为空字符串以传递回父级。

问题是在从行中提取值并设置为变量之前执行了 segue,因此传递回父级的值是默认值 "fuckdickdonteattidepods"。

我完全被困在这个问题上,任何建议将不胜感激!

好吧,我首先想到的是删除展开转场,使用 unwindSegueIdentifier 创建手动展开转场 (see this answer),然后在 [=12= 中以编程方式调用它]:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    favoriteArtist = (names[indexPath.row])
    print(favoriteArtist)
    self.performSegue(withIdentifier: "unwindSegueIdentifier", sender: self)
}