3 回答

TA貢獻1815條經驗 獲得超6個贊
Swift的工作方式與Obj-C完全相同,但在新語言中重新編寫。我的帖子中沒有很多信息,但是讓我們為每個TableViewController命名,以幫助我解釋。
HomeTableViewController(這是你上面的截圖)
PlayerTableViewController(這是你想去的玩家屏幕)
話雖如此,在PlayerTableViewController中你需要有一個存儲傳遞數據的變量。在你的類聲明下就有這樣的東西(如果你打算將結構存儲為單個對象而不是數組:
class PlayerTableViewController: UITableViewController { var programVar : Program? //the rest of the class methods....
之后,有兩種方法可以將數據發送到新的TableViewController。
1)使用prepareForSegue
在HomeTableViewController的底部,您將使用prepareForSegue方法傳遞數據。以下是您將使用的代碼示例:
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加載,變量將已經設置并可用
2)使用didSelectRowAtIndexPath
如果需要根據選擇的單元格發送特定數據,則可以使用didSelectRowAtIndexPath。為此,您需要在故事板視圖中為您的segue命名(如果您需要知道如何執行此操作,請告訴我)。
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貢獻1803條經驗 獲得超6個贊
使用Swift 3和4
在第一個ViewController中(發送值)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if (segue.identifier == "MainToTimer") { let vc = segue.destination as! YourViewController vc.verificationId = "Your Data" }}
在第二個viewController(Catch The Value)中
var verificationId = String()

TA貢獻1998條經驗 獲得超6個贊
如果您不需要通過標識符識別操作,只能通過目標類識別...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let vc = segue.destination as? YourViewController { vc.var_name = "Your Data" }}
- 3 回答
- 0 關注
- 598 瀏覽
添加回答
舉報