如何将数据从一个视图控制器传递到另一个SWIFT [英] How to pass data from one view controller to the other SWIFT

查看:101
本文介绍了如何将数据从一个视图控制器传递到另一个SWIFT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,其中带有搜索栏和范围栏的表格视图必须与详细视图控制器连接,并且该详细视图控制器必须根据选择的单元格显示数据.我有一个带有结构的数组,用于对项目进行排序和搜索.我需要保留此功能,我的详细信息视图控制器还有另一个swift类,我将把if/else语句放到该类中,以基于哪个单元格显示数据 我选择.我需要知道的操作是将一个变量附加到单元格上,然后将该变量传递给详细信息视图控制器以在我的if/else语句中使用,以便显示数据.如果这不是正确的方法,请告诉我.

I am making an app where a table view with a search bar and scope bar has to segue to a detail view controller and that detail view controller has to display data based on which cell is selected. I have an array with structs set up to sort and search for the items. I need to keep this feature, I have another swift class for my detail view controller that I will put if/else statements into that deal with displaying the data based on what cell I select. What I need to know how to do is attach a variable to the cells and pass that variable to the detail view controller to use in my if/else statements so that I can display the data. If this is not the correct way to do this, please let me know.

结构代码

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中,创建一个变量,该变量包含当前选定的Booth,并在didSelectRowAtIndexPath中分配该变量.然后,您可以在prepareForSegue方法中将此变量传递到详细信息视图控制器.

For instance, in your BoothsTableViewController create variable holding the currently selected Booth, which you assign in didSelectRowAtIndexPath. You can then pass this variable to the detail view controller in your prepareForSegue method.

这篇关于如何将数据从一个视图控制器传递到另一个SWIFT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆