如何将数据从一个视图控制器传递到另一个SWIFT
问题描述:
我正在制作一个应用程序,其中带有搜索栏和范围栏的表视图必须继承到详细视图控制器,并且该详细视图控制器必须基于哪个单元格被选中。我有一个数组,其结构设置为排序和搜索项目。我需要保持这个功能,我有另一个我的详细信息视图控制器的swift类,我将把if/else语句放入显示基于我选择的单元格的数据的处理中。我需要知道如何做的是将一个变量附加到单元格中,并将该变量传递给详细视图控制器以在我的if/else语句中使用,以便我可以显示数据。如果这不是这样做的正确方法,请让我知道。如何将数据从一个视图控制器传递到另一个SWIFT
结构代码
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
创建变量保存当前选定展位,您指定在didSelectRowAtIndexPath
。然后,您可以将此变量传递给您的prepareForSegue
方法中的详细视图控制器。
答
在detailViewController中创建成员变量/对象。在您当前的TableViewController中的prepareforSegue中设置此对象。这样detailViewController将拥有用户点击的单元格对象。
是的,谢谢,事情是我不知道如何传递它@Rick – user3558131 2014-10-30 20:25:58
只需在详细视图中创建一个变量,您可以在创建'DetailViewController'后设置变量,就像你一样与标题做。 – Rick 2014-10-30 20:27:08