如何在swift中使用prepareForSegue?

时间:2016-03-23 09:02:48

标签: ios xcode swift segue

我有一个带有tableview的ViewController,名为BasicPhrasesVC,我希望传递所选单元格中的数据,以便在下一个ViewController(名为BasicPhrasesVC)上显示它。

class BasicPhrasesVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

let basicPhrases = ["Hello.","Goodbye.","Yes.","No.","I don't understand.","Please?","Thank you.","I don't know."]
var selectedBasicPhrase = ""

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

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


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")!
    cell.textLabel?.text = basicPhrases[indexPath.row]
    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

我不确定放在这里的内容(我想传递变量" selectedBasicPhrase")

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedBasicPhrase = basicPhrases[indexPath.row]
    performSegueWithIdentifier("BasicPhrasesVC2BasicDisplayVC", sender: self)

}
}

感谢任何帮助。

3 个答案:

答案 0 :(得分:9)

const query = 'SELECT * FROM events.by_hour WHERE level IN ? and ' + 
              'hour IN ? and userid = ?';
const params = [ [ 10, 20, 30] , [ 2016032117, 2016032118 ],'user_uuid' ];
client.execute(query, params, { prepare: true }, callback);

答案 1 :(得分:0)

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "<segue name>") {
        // pass the data
    }
}

答案 2 :(得分:0)

检查segue名称而不是destinationViewController,然后将数据发送给它。

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "BasicPhrasesVC2BasicDisplayVC") {
        let viewController:ViewController = segue!.destinationViewController as ViewController
        viewController.selectedBasicPhrase = "Test phrase"
    }
}