swift ios在控制器之间传递数据

时间:2016-03-24 22:47:40

标签: ios swift segue

我试图在两个控制器之间传递数据,更准确地说,有一个按钮从UIViewController到具有变量datadefine的UITableViewController,当我尝试打印datadefine变量时,它总是空白虽然我正在设置它在prepareForSegue函数中,下面是相同的代码

//
//  VC1.swift
//  TableViewSearchInSwift
//
//  Created by melwyn.p on 24/03/16.
//  Copyright © 2016 TheAppGururz. All rights reserved.
//

import UIKit

class VC1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let dataViewController = segue.destinationViewController as! SearchViewController
        dataViewController.defineData = "data1"  
        print("test")      
    } 

}

下面是searchViewController的代码,这也是最初的主视图,它已被切换为第二个视图,现在弹出按钮点击

//
//  SearchViewController.swift
//  TableViewSearchInSwift
//
//  Created by TheAppGuruz-iOS-103 on 24/12/15.
//  Copyright © 2015 TheAppGururz. All rights reserved.
//

import UIKit

class SearchViewController: UIViewController, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate
{

    var marrCountryList = [String]()
    var marrFilteredCountryList = [String]()
    var defineData  =   String()
    var file    =   String()


    @IBOutlet var tblCountry: UITableView!

    override func viewDidLoad()
    {
        print(defineData)

        super.viewDidLoad()
        var text=""


        print(defineData)




        if  defineData == "data1"
        {
             file = "/data1.txt"
        }
        else
        {
             file = "/data2.txt"
        }


        do{
              text = try String(contentsOfFile: file, encoding: NSUTF8StringEncoding)



                    }
        catch
        {
            print(error);
            //contentsOfFile = nil;
        } 
        marrCountryList = text.componentsSeparatedByString("\n")
        self.tblCountry.reloadData()
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        if tableView == self.searchDisplayController!.searchResultsTableView {
            return self.marrFilteredCountryList.count
        } else {
            return self.marrCountryList.count
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cellCountry = self.tblCountry.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

        var countryName : String!
        if tableView == self.searchDisplayController!.searchResultsTableView {
            countryName = marrFilteredCountryList[indexPath.row]
        } else {
            countryName = marrCountryList[indexPath.row]
        }

        cellCountry.textLabel?.text = countryName.componentsSeparatedByString("%%")[0]

        return cellCountry
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        print(defineData)
    }

    func filterTableViewForEnterText(searchText: String) {
        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchText)

        let array = (self.marrCountryList as NSArray).filteredArrayUsingPredicate(searchPredicate)
        self.marrFilteredCountryList = array as! [String]
        self.tblCountry.reloadData()
    }


    func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String?) -> Bool {
        self.filterTableViewForEnterText(searchString!)
        return true
    }

    func searchDisplayController(controller: UISearchDisplayController,
        shouldReloadTableForSearchScope searchOption: Int) -> Bool {
            self.filterTableViewForEnterText(self.searchDisplayController!.searchBar.text!)
            return true
    }
}

2 个答案:

答案 0 :(得分:-3)

您无法在viewDidLoad中打印它。 viewDidLoad只执行一次,它发生在prepareForSeque触发之前。将其移至viewWillAppear。

override func viewWillAppear(animated: Bool) {
    print(defineData)    
}

答案 1 :(得分:-3)

你应该使用在存储新值后立即调用的didSet内部函数。

在SearchViewController类上尝试:

var defineData: String! {
    didSet(defineData) {
        self.defineData = defineData
    }
}

当你这样做时:

let dataViewController = segue.destinationViewController as! SearchViewController
dataViewController.defineData = "data1"

dataViewController变量不是实际对象 这就是为什么你无法比较这个

dataViewController.defineData = "data1"

因为defineData对象的dataViewController不存在

但是当你使用didSet内部函数时,你会说在dataViewController对象是实际对象后立即设置该值。

如果您想了解更多信息,请查看以下链接:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-ID254