swift - 以编程方式触发的 segue 后显示的 View 不会立即更新

标签 swift ios10

我创建了两个 View ,然后通过按住 Ctrl 并拖动并创建一个 segue 将一个 View 连接到另一个 View 。给这个 segue 标识符“functionOutput1”

然后我创建了一个函数,并根据函数中的特定输出以编程方式触发上面创建的 segue。

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

View 继续正常,但 View 的内容直到一分钟后才出现。

我还需要在这里做些什么吗?

编辑: 我在 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)
                }
            }
        })

最佳答案

这可能是线程问题。因为数据是异步返回的。您需要在主线程上运行 View 更新。您可以使用以下代码在 Swift 3.0 中执行此操作:

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

关于swift - 以编程方式触发的 segue 后显示的 View 不会立即更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40229396/

相关文章:

iOS 10 MapKit 上层缩放问题

ios - locationManager 在 Swift 3 中被弃用了吗?

ios - xcode 8 和 swift 3 - PHPhotoLibrary.requestAuthorization 崩溃

ios - Firebase 数据库,权限被拒绝(无规则)swift

ios - 使用 CoreBluetooth/L2CAP channel 传输数据

iOS 应用程序在 PushViewController 上卡住

ios - 当 iOS 10 中的 UNUserNotificationCenterDelegate 收到新通知时,如何删除以前发送的通知?

ios - UITableView:从 TableViewController 中分离数据源并分配#selector

ios - Swift 中文本字段或 TextView 中的多个关键字

ios - SwiftUI - 带有图像的按钮可在外部点击