加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 如何在swift中使用prepareForSegue?

发布时间:2020-12-14 19:25:58 所属栏目:百科 来源:网络整理
导读:我有一个带有tableview的ViewController,名为BasicPhrasesVC,我想传递所选单元格中的数据,以便在下一个ViewController(称为BasicPhrasesVC)上显示它. class BasicPhrasesVC: UIViewController,UITableViewDataSource,UITableViewDelegate {let basicPhrases
我有一个带有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)

}
}

任何帮助表示赞赏.

解决方法

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

}

override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
        if segue.identifier == "BasicPhrasesVC2BasicDisplayVC" {
            if let nextVC = segue.destinationViewController as? NextViewController {
                nextVC.selectedBasicPhrase = sender
            }
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读