ios - 如何在 swift 中使用 prepareForSegue?

标签 ios xcode swift segue

我有一个带有名为 BasicPhrasesVC 的 TableView 的 ViewController,我想传递所选单元格中的数据以在下一个 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
            }
        }
    }

关于ios - 如何在 swift 中使用 prepareForSegue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36173734/

相关文章:

ios - iOS 13中的SceneDelegate,是否有必要?

ios - 在 UItextfield 的右 View 上添加一个按钮,文本不应与按钮重叠

swift - 将 html 转换为文本(空格问题)

ios - 使用 Swift SpriteKit 时出现错误 "Thread 1: EXC_BAD_INSTRUCTION (code = EXC_1386_INVOP, subcode = 0x0)"

ios - 清除 contenteditable div 时,键盘在 Safari iOS 6 中停止工作

ios - 在已经出现后更改动态高度 UITableViewCell 的高度

ios - 限制目标在 iOS 项目中使用的本地化

ios - 无法在 Xcode9 Swift4 中使用 alamofire 类和方法

iphone - 如何在不使用按钮的情况下移动到另一个 View

ios - 为什么 CollectionView 中的自定义按钮只加载到最后一个单元格中? ( swift )