如何将数据从一个视图控制器传递到另一个SWIFT

我正在制作一个应用程序,其中带有search栏和范围栏的表格视图必须延伸到详细信息视图控制器,并且该详细信息视图控制器必须根据所选单元格显示数据。 我有一个数组结构来设置sorting和search的项目。 我需要保持这个function,我有我的详细信息视图控制器的另一个Swift类,我将放入if / else语句到显示基于我select的单元格的数据处理。 我需要知道如何做的是将一个variables附加到单元格,并将该variables传递给详细视图控制器以在我的if / else语句中使用,以便我可以显示数据。 如果这不是正确的方法,请让我知道。

结构代码

struct Booth { let category : String let name : String } 

表视图控制器代码

 import UIKit class BoothsTableViewController: UITableViewController { var booths = [Booth]() var filteredBooths = [Booth]() override func viewDidLoad() { super.viewDidLoad() //fill array with data self.booths = [Booth(category: "Tech", name: "Conference App"), Booth(category: "Tech", name: "Space Shooter"), Booth(category: "Tech", name: "RollABall"), Booth(category: "Animation", name: "Sugar Hill City Model"), Booth(category: "Animation", name: "3D Sculpting 101"), Booth(category: "Animation", name: "HowTo - Texture"), Booth(category: "Science", name: "AP Biology for Dummies"), Booth(category: "Science", name: "Cells"), Booth(category: "Science", name: "Space")] //reload the table self.tableView.reloadData() } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if tableView == self.searchDisplayController!.searchResultsTableView { return self.filteredBooths.count } else { return self.booths.count } } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell var boothsCheck : Booth // Check to see whether the normal table or search results table is being displayed and set the Booth object from the appropriate array if tableView == self.searchDisplayController!.searchResultsTableView { boothsCheck = filteredBooths[indexPath.row] } else { boothsCheck = booths[indexPath.row] } // Configure the cell cell.textLabel.text = boothsCheck.name cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator return cell } func filterContentForSearchText(searchText: String, scope: String = "All") { self.filteredBooths = self.booths.filter({( Booth : Booth) -> Bool in var categoryMatch = (scope == "All") || (Booth.category == scope) var stringMatch = Booth.name.rangeOfString(searchText) return categoryMatch && (stringMatch != nil) }) } func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool { let scopes = self.searchDisplayController!.searchBar.scopeButtonTitles as [String] let selectedScope = scopes[self.searchDisplayController!.searchBar.selectedScopeButtonIndex] as String self.filterContentForSearchText(searchString, scope: selectedScope) return true } func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchScope searchOption: Int) -> Bool { let scope = self.searchDisplayController!.searchBar.scopeButtonTitles as [String] self.filterContentForSearchText(self.searchDisplayController!.searchBar.text, scope: scope[searchOption]) return true } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { self.performSegueWithIdentifier("BoothDetail", sender: tableView) } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "BoothDetail" { let BoothDetailViewController = segue.destinationViewController as UIViewController if sender as UITableView == self.searchDisplayController!.searchResultsTableView { let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()! let destinationTitle = self.filteredBooths[indexPath.row].name BoothDetailViewController.title = destinationTitle } else { let indexPath = self.tableView.indexPathForSelectedRow()! let destinationTitle = self.booths[indexPath.row].name BoothDetailViewController.title = destinationTitle } } } } 

Segue代码

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "BoothDetail" { let BoothDetailViewController = segue.destinationViewController as UIViewController if sender as UITableView == self.searchDisplayController!.searchResultsTableView { let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()! let destinationTitle = self.filteredBooths[indexPath.row].name BoothDetailViewController.title = destinationTitle } else { let indexPath = self.tableView.indexPathForSelectedRow()! let destinationTitle = self.booths[indexPath.row].name BoothDetailViewController.title = destinationTitle } } } } 

例如,在你的BoothsTableViewController创build一个variables来保存你在didSelectRowAtIndexPath分配的当前select的Booth。 然后,您可以将这个variables传递给prepareForSegue方法中的详细视图控制器。

在detailViewController中创build一个成员variables/对象。 在您当前的TableViewController中的prepareforSegue中设置此对象。 这样detailViewController将有用户点击的单元格对象。