在模态视图控制器中放松发生太早的争论

时间:2018-01-24 11:28:14

标签: ios swift modalviewcontroller unwind-segue

我有一个包含以下代码的父视图控制器:

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

我将使用以下代码以模态方式呈现表视图控制器:

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

我设置了这样,当用户从表格视图中选择一个单元格时,会执行展开segue以返回到父vc。

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

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

我完全坚持这个问题,非常感谢任何建议!

1 个答案:

答案 0 :(得分:0)

好吧,我想到的第一件事就是删除unwind segue,用unwindSegueIdentifier创建一个手动展开segue(see this answer),然后在didSelectRowAt中以编程方式调用它:

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