从带有TableView的ControlView获取字符串

3
我成功地设置了我的tableView,使其可以根据点击的行正确地传递特定的字符串。然而,我不知道如何检索这些数据。我知道如何在Java中执行此操作,但我对Swift还很陌生,感到困惑。
发送方控制视图:
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"
    {

}

IndividualTechniqueController是什么?它是当前的控制器吗?另外,您只想从cellContent数组中传递字符串对象吗?还请展示您创建的故事板segue及其标识符。 - Nirav D
是的,我更新了 NDoc。 - ESPA_NETwork
@NDoc,我对segue有一些了解。我需要它来完成我的任务吗? - ESPA_NETwork
@GrantEspanet 请查看此链接 https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/Chapters/StoryboardSegue.html 在此链接中,segue 是在单元格和视图控制器之间创建的,您需要在两个控制器之间创建segue,即从“TechniqueListViewController”到“IndividualTechniqueController”,然后设置segue标识符。完成后,请通知我并显示屏幕截图,以便我可以检查是否已正确完成。 - Nirav D
@NDoc,我注意到在按钮、导航栏项目等方面使用segue拖动是可以的。但是对于表视图来说,它不起作用。 - ESPA_NETwork
显示剩余9条评论
1个回答

1
在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"
 {

  }
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接