视图控制器之间传递数据的差异

时间:2016-03-02 00:38:31

标签: ios json swift

我正在尝试在我的第一个ViewController和我的SecondViewController之间传递数据。我在ViewController的顶部有两个初始化为空的变量。然后,我获得解析JSON数据并将这两个变量的值设置为已解析的临时JSON变量。然后我在prepareforSegue函数中,让SecondViewController的变量等于我的第一个ViewController中的前两个变量。由于某种原因,传递的变量仅在变量为空时传递变量。我不确定我是否有任何意义,但这是我的代码:

class ViewController: UIViewController {

var stockSymbol = String()
var stockPrice = String()

@IBOutlet weak var textField: UITextField!

@IBAction func buttonPressed(sender: AnyObject) {

    let requestURL: NSURL = NSURL(string: "https://finance.yahoo.com/webservice/v1/symbols/\(textField.text!)/quote?format=json")!
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(urlRequest) {
        (data, response, error) -> Void in

        let httpResponse = response as! NSHTTPURLResponse
        let statusCode = httpResponse.statusCode

        if (statusCode == 200) {
            print("Everyone is fine, file downloaded successfully.")

            do {

                let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)

                if let resources = json["list"]?!["resources"] as? [[String:AnyObject]] {
                    if let fields = resources[0]["resource"]?["fields"] as? [String:String], price = fields["price"], symbol = fields["symbol"] {

                        self.stockSymbol = symbol
                        self.stockPrice = price

                        print(self.stockSymbol, self.stockPrice)
                    } 
                }

            } catch {
                print("Error with Json: \(error)")

                //end catch
            }

        } //end if status code == 200
    } //end task


    task.resume()

}

override func viewDidLoad() {
    super.viewDidLoad()

    //var test:String

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "searchResults") {

        print("working \(self.stockPrice)")
        let secondVC = segue.destinationViewController as! SecondViewController;
        secondVC.passedSymbol = self.stockSymbol
        secondVC.passedPrice = self.stockPrice
        print(secondVC.passedSymbol)



    }
}

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


}

基本上我希望股票价格和符号转移到我的第二个视图控制器的变量,但它似乎不起作用。

第二视图控制器代码:

class SecondViewController: UIViewController {

var passedSymbol: String!
var passedPrice: String!

@IBOutlet weak var symbol: UILabel!

@IBOutlet weak var price: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    print("passed \(passedSymbol)")
    print("second view")
    dispatch_async(dispatch_get_main_queue()) {
    self.symbol.text = self.passedSymbol
    self.price.text = self.passedPrice

    }

}

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


}

将“aapl”放入searchField

时的输出语句
working 

passed 
second view
Everyone is fine, file downloaded successfully.
AAPL 100.529999

2 个答案:

答案 0 :(得分:2)

首先,不要这样做:

var stockSymbol = String()
var stockPrice = String()

这是一个坏习惯,可以阻止编译器抱怨未初始化的变量,但完全忽略了Swift中选项的强大功能。

相反,这样做:

var stockSymbol:String!
var stockPrice:String!

这将确保您在不设置值的情况下尝试使用变量时会出现异常。

现在,您获得空值的原因(或者如果您进行了我的建议更改会得到异常)是您在按下按钮时启动异步数据提取但立即启动segue,可能是因为您有与按钮以及touchUpInside处理程序关联的操作。

您需要删除动作segue并通过从场景顶部的黄色View Controller图标拖动到目标视图控制器来创建程序化segue,并像往常一样为新segue提供标识符。 / p>

然后,您可以更改buttonPressed函数,以便在检索到数据后调用segue;

@IBAction func buttonPressed(sender: AnyObject) {

    let requestURL: NSURL = NSURL(string: "https://finance.yahoo.com/webservice/v1/symbols/\(textField.text!)/quote?format=json")!
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(urlRequest) {
        (data, response, error) -> Void in

        let httpResponse = response as! NSHTTPURLResponse
        let statusCode = httpResponse.statusCode

        if (statusCode == 200) {
            print("Everyone is fine, file downloaded successfully.")

            do {

                let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)

                if let resources = json["list"]?!["resources"] as? [[String:AnyObject]] {
                    if let fields = resources[0]["resource"]?["fields"] as? [String:String], price = fields["price"], symbol = fields["symbol"] {

                        self.stockSymbol = symbol
                        self.stockPrice = price

                        print(self.stockSymbol, self.stockPrice)
                        dispatch_async(dispatch_get_main_queue(), {
                            self.performSegueWithIdentifier("searchResults",sender:sender)
                        })
                    } 
                }

            } catch {
                print("Error with Json: \(error)")

                //end catch
            }

        } //end if status code == 200
    } //end task


    task.resume()

}

答案 1 :(得分:1)

原因应该是当你去第二个viewcontroller时,你没有第一个viewcontroller中的数据。 dataTaskWithRequest是一个异步函数,这意味着它在另一个线程而不是主ui线程上运行。你可以做的就是等到你在第一个viewcontroller上获取数据然后调用segue。控制台上的打印信息可能会让您感到困惑,就像您已经获得了数据一样,但实际上它可能在调用segue之后发生并且导致没有数据通过。