我看到的是segue正在发生,并且在我的请求完成并解析响应之前,下一个VC正在传递一个空数组。 (这完全基于我在几乎所有其他行中插入的打印语句来跟踪我的代码的执行)。
我怎样才能解决这个问题,以便在我的响应被解析之前,segue不会将我带到下一个VC?
class start: UIViewController, NSURLConnectionDelegate, NSXMLParserDelegate {
var timeArr:[String] = []
var url = "myurl.com"
@IBOutlet weak var get2: UIButton!
@IBAction func getAction(sender: UIButton) {
var request = NSMutableURLRequest(URL: NSURL(string: url)!)
httpGet (request) {
(data, error) -> Void in
if error != nil {
print(error)
mainInstance.arrivalArr.append("no arrival times")
} else {
self.beginParsing()
print(data)
}
}
}
func httpGet(request: NSURLRequest!, callback: (String, String?) -> Void) {
var session = NSURLSession.sharedSession()
var task = session.dataTaskWithRequest(request){
(data, response, error) -> Void in
if error != nil {
callback("", error!.localizedDescription)
} else {
var result = NSString(data: data!, encoding:
NSUTF8StringEncoding)!
self.data2 = data!
callback(result as String, nil)
}
}
task.resume()
}
func beginParsing()
{
posts = []
parser = NSXMLParser(data: data2)
parser.delegate = self
parser.parse()
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
{
element = elementName
if (elementName as NSString).isEqualToString("prd")
{
// more code
}
}
func parser(parser: NSXMLParser, foundCharacters string: String)
{
if element.isEqualToString("prdtm") {
//code
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
if (elementName as NSString).isEqualToString("prd") {
if !title1.isEqual(nil) {
//code
}
//code
finalTimeStr = getTime(finalTimeStr)
timeArr.append(finalTimeStr)
}
}
func getTime (time: String) -> String{
var arrivalTime = time
//code to trim and format time here
var timeA = getTimeDiff(arrivalTime)
timeA = trimToMin(timeA)
return timeA
}
func trimToMin (timeToTrim : String) -> String {
var timeInMin = timeToTrim
//code to trim to minutes
return substring2
}
func getTimeDiff (time: String) -> String{
//code to find time difference
let ret = stringFromTimeInterval(diff)
return ret
}
func stringFromTimeInterval(interval: NSTimeInterval) -> String {
//code to format interval
return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let CDview = segue.destinationViewController as? ListView {
CDview.timeArr = timeArr
//CDview.timeArr = mainInstance.arrivalArr
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
timeArr = testArr
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
} 不知道我哪里出错了。它特别令人困惑,因为如果我使用硬编码字符串并直接跳入getTimeDiff然后它工作得非常好,我对下一个VC的表视图看起来也很棒。非常感谢任何帮助。在过去的12个小时里,我试图理解为什么它不起作用。
答案 0 :(得分:1)
在情节提要中从视图控制器(而不是UIButton)添加segue。并根据需要命名(故事板标识符)。解析数据后,使用performSegue(...)以编程方式执行segue。