有谁知道如何解决这个问题?
问题是prepareForSegue
函数没有获得chanceRain
变量。由于某种原因chanceRain
只在任务变量中有一个值。有人知道如何解决这个问题吗?我试图在游乐场修复它但我不能在其中导入SWXMLHash。
import UIKit
import SWXMLHash
class ViewController: UIViewController, UITableViewDelegate {
var cellContent = [""]
var indexOfTime = 0
var chanceRain = ""
var defaults = NSUserDefaults(suiteName: "group.com.royvano.paraplu")
var rainInt :Int = 0
@IBOutlet var cityInput: UITextField!
func enumerate(indexer: XMLIndexer) {
for child in indexer.children {
NSLog(child.element!.name)
enumerate(child)
}
}
@IBAction func button(sender: AnyObject) {
var url = NSURL(string: "http://api.worldweatheronline.com/free/v2/weather.ashx?key=ca89103dfe0a8b844a0a1e1249820&q=" +
cityInput.text.stringByReplacingOccurrencesOfString(" ", withString: "-") + "/" + cityInput.text.stringByReplacingOccurrencesOfString(" ", withString: "-") + "/" + "&num_of_days=2&tp=3&format=xml")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding)
println(url)
var xml = SWXMLHash.parse(urlContent!)
for elem in xml["data"]["weather"][0]["hourly"][self.indexOfTime]{
self.chanceRain = (elem["chanceofrain"].element!.text!)
self.defaults?.setObject(self.chanceRain, forKey: "rainPercentage")
self.defaults?.synchronize()
self.performSegueWithIdentifier("btnSubmitSegue", sender: self)
}
println(self.chanceRain)
})
task.resume()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "btnSubmitSegue"){
var svc: viewTwo = segue.destinationViewController as viewTwo
svc.datapassed = "There is " + self.chanceRain + "% chance of rain" //dit werkt, maar chanceRain niet..?
} }
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
cellContent = ["01:00","04:00","07:00", "10:00", "13:00", "16:00", "19:00", "22:00"]
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellContent.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default , reuseIdentifier: "Cell")
cell.textLabel?.text = "\(cellContent[indexPath.row])"
return cell }
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
indexOfTime = indexPath.row
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
此处的viewTwo类:
import Foundation
import UIKit
class viewTwo: UIViewController {
@IBOutlet var label: UILabel!
var datapassed:String! = ""
override func viewDidLoad() {
label.text = datapassed }
}
答案 0 :(得分:0)
由于除了IBAction button(_:)
之外,您在按下按钮时触发了segue,因此当发生segue时,chanceOfRain
变量不会被更新。这是因为chanceOfRain
在异步调用的块中更新,这意味着需要时间来更新。到更新时,prepareForSegue
可能已经解决。
要解决此问题,请不要在故事板中按下按钮上的segue。相反,使它成为从第一个ViewController到第二个具有标识符的ViewController的常规segue。然后,在知道performSegueWithIdentifier(_:sender:)
已更新后,您可以在异步调用的块中调用chanceOfRain
。这将确保数据正确传递。