我成功设置了表格视图,以便它可以根据所点击的行正确地传递特定的String。但是,我不知道如何检索此数据。我知道如何在Java中执行此操作,但是我是新手,很快就发现它令人困惑。

发件人ControlView:

import UIKit

class TechniqueListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let cellContent = ["Stance", "Move Forward", "Move Backward", "Move Right", "Move Left", "Jab", "Cross", "Hook", "Uppercut", "Body Jab", "Body Cross", "Body Hook", "Body Uppercut", "Leg Kick", "Body Kick", "Switching Stances", "Switch Leg Kick", "Switch Body Kick", "Push Kick", "Switch Push Kick", "Front Push Kick", "Switch Front Push Kick", "Spinning Back Kick", "Knee", "Switch Knee", "Elbow", "Tornado Kick"]

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        //gets # of row
        return cellContent.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        //defines content of each cell

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "TechniqueCell")
        cell.textLabel?.text = cellContent[indexPath.row]

        return cell

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cellIndex = indexPath.row
        if (cellIndex == 0){
            performSegue(withIdentifier: value(forKey: "Stance") as! String, sender: IndividualTechniqueController())
        }
        else if cellIndex == 1{
            performSegue(withIdentifier: "Move Forward", sender: IndividualTechniqueController())
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

检索ControlView:
    //I want an if else statement here
    if senderString == "Stance"{  //  <---- correct me if this is wrong
    }
    else if senderString == "Move Forward"
    {

}

最佳答案

您好在TechniqueListViewController中采用数据类型为String(您所需的数据类型)的一个变量,如下所示

var previouspageData: String

在tableview didselect方法中使用该变量可以将数据发送到该控制器
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

self.previouspageData = "your data"
 let cellIndex = indexPath.row
if (cellIndex == 0){
    performSegue(withIdentifier: value(forKey: "Stance") as! String, sender:self)
}
else if cellIndex == 1{
     performSegue(withIdentifier: "Move Forward", sender:self)
}
}

使用该数据作为目的地控制者
 override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) {
if sender is TechniqueListViewController {
 if  (sender as! TechniqueListViewController).previouspageData == "Stance"{
 }
 else if (sender as! TechniqueListViewController).previouspageData == "Move Forward"
 {

  }
}
}

10-06 01:11