以编程方式触发的 segue 后显示的视图不会立即更新



我创建了两个视图,然后通过ctrl拖动和创建segue将一个视图连接到另一个视图。给这个segue指定标识符"functionOutput1"

然后我创建了一个函数,并基于函数中的某些输出以编程方式触发了上面创建的segue。

performSegue(withIdentifier: "functionOuput1", sender: self)

视图可以正常segue,但是视图的内容一分钟后才出现。

这里还有什么我需要做的吗?

编辑:我在URLRequest

的回调函数中执行这个segue
        let task = session.dataTask(with: request as URLRequest, completionHandler: {
            data, response, error -> Void in
            if (error != nil) {
                // there is an error with the request
                print("error: ", error)
            } else {
                // Request was successful. Check the contents of the response.
                print("response: ", response)
                let httpResponse = response as! HTTPURLResponse
                if httpResponse.statusCode != 200 {
                    print("problems with server request")
                } else {
                    print("request successful")
                    //go to next page
                    performSegue(withIdentifier: "functionOuput1", sender: self)
                }
            }
        })

这可能是线程问题。因为数据是异步返回的。您需要在主线程上运行视图更新。你可以在Swift 3.0中使用下面的代码来实现这一点:

DispatchQueue.main.async {
    // Your view updates here                
}

最新更新