Swift hoạt động chính xác theo cùng một cách như obj-C nhưng được làm lại bằng ngôn ngữ mới. Tôi không có nhiều thông tin từ bài viết của bạn nhưng hãy đặt tên cho mỗi TableViewController để giúp giải thích của tôi.
HomeTableViewController (đây là ảnh chụp màn hình bạn có ở trên)
PlayerTableViewController (đây là màn hình máy nghe nhạc bạn muốn đi du lịch đến)
Với những gì đã nói, trong PlayerTableViewController bạn cần phải có một biến sẽ lưu trữ dữ liệu được truyền. Chỉ dưới khai báo lớp bạn có một cái gì đó như thế này (nếu bạn có ý định để lưu trữ các cấu trúc như một đối tượng duy nhất chứ không phải là mảng:.
class PlayerTableViewController: UITableViewController {
var programVar : Program?
//the rest of the class methods....
Sau đó có hai cách bạn có thể gửi dữ liệu đến TableViewController mới
1) sử dụng prepareForSegue
Ở dưới cùng của HomeTableViewController bạn sẽ sử dụng các phương pháp prepareForSegue để vượt qua các dữ liệu. Dưới đây là một ví dụ về mã bạn sẽ sử dụng:
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
}
}
Khi PlayerTableViewController đã nạp các biến sẽ được đã được thiết lập và sử dụng
2) Sử dụng didSelectRowAtIndexPath
Nếu dữ liệu cụ thể cần được gửi dựa trên ô nào được chọn, bạn có thể sử dụng didSelectRowAtIndexPath. Để làm việc này, bạn cần phải đặt tên segue của bạn trong khung nhìn storyboard (cho tôi biết nếu bạn cần biết cách thực hiện điều này).
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)
}
Hãy cho tôi biết nếu bạn cần bất kỳ thông tin nào khác về vấn đề này
nhưng tôi nghĩ rằng đây là trong Objective-C và tôi cần nó trong nhanh chóng. – patrikbelis
có thể trùng lặp của [Chuyển các biến từ một ViewController sang một biến khác trong Swift] (http://stackoverflow.com/questions/24044108/pass-variables-from-one-viewcontroller-to-another-in-swift) – Paulw11