3 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
Swift的工作方式與Obj-C完全相同,但在新語言中重新編寫。我的帖子中沒有很多信息,但是讓我們?yōu)槊總€(gè)TableViewController命名,以幫助我解釋。
HomeTableViewController(這是你上面的截圖)
PlayerTableViewController(這是你想去的玩家屏幕)
話雖如此,在PlayerTableViewController中你需要有一個(gè)存儲(chǔ)傳遞數(shù)據(jù)的變量。在你的類聲明下就有這樣的東西(如果你打算將結(jié)構(gòu)存儲(chǔ)為單個(gè)對象而不是數(shù)組:
class PlayerTableViewController: UITableViewController { var programVar : Program? //the rest of the class methods....
之后,有兩種方法可以將數(shù)據(jù)發(fā)送到新的TableViewController。
1)使用prepareForSegue
在HomeTableViewController的底部,您將使用prepareForSegue方法傳遞數(shù)據(jù)。以下是您將使用的代碼示例:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { // Create a variable that you want to send var newProgramVar = Program(category: "Some", name: "Text") // Create a new variable to store the instance of PlayerTableViewController let destinationVC = segue.destinationViewController as PlayerTableViewController destinationVC.programVar = newProgramVar }}
一旦PlayerTableViewController加載,變量將已經(jīng)設(shè)置并可用
2)使用didSelectRowAtIndexPath
如果需要根據(jù)選擇的單元格發(fā)送特定數(shù)據(jù),則可以使用didSelectRowAtIndexPath。為此,您需要在故事板視圖中為您的segue命名(如果您需要知道如何執(zhí)行此操作,請告訴我)。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { // Create a variable that you want to send based on the destination view controller // You can get a reference to the data by using indexPath shown below let selectedProgram = programy[indexPath.row] // Create an instance of PlayerTableViewController and pass the variable let destinationVC = PlayerTableViewController() destinationVC.programVar = selectedProgram // Let's assume that the segue name is called playerSegue // This will perform the segue and pre-load the variable for you to use destinationVC.performSegueWithIdentifier("playerSegue", sender: self)}
如果您需要任何其他信息,請告訴我

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊
使用Swift 3和4
在第一個(gè)ViewController中(發(fā)送值)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if (segue.identifier == "MainToTimer") { let vc = segue.destination as! YourViewController vc.verificationId = "Your Data" }}
在第二個(gè)viewController(Catch The Value)中
var verificationId = String()

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果您不需要通過標(biāo)識(shí)符識(shí)別操作,只能通過目標(biāo)類識(shí)別...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let vc = segue.destination as? YourViewController { vc.var_name = "Your Data" }}
- 3 回答
- 0 關(guān)注
- 597 瀏覽
添加回答
舉報(bào)