Swift 3 : get information from server in ViewDidLoad and when click a button, 等待服务器的信息响应

标签 swift xcode asynchronous alamofire

在我的应用程序中,我想在 ViewDidLoad 中调用异步函数(使用 alamofire),并且用户仍然可以与 View 交互,直到按下按钮,并且如果异步函数不是,我希望应用程序显示进度 hud已完成或如果已完成则转到下一个 View 。我不想在单击按钮时调用异步函数,因为与我在 ViewDidLoad 调用异步函数相比,有时可能需要等待更长的时间 我怎样才能做到这一点?

最佳答案

编辑 - 使用枚举来保存 View Controller 状态

enum State {
    case Loading
    case Ready
    case ShowNext
}
var state: State

func viewDidLoad() {
  state = .Loading
  makeAsyncCall() { (finished) in 
     if state == .ShowNext {
         showNext()
     } else {
         state = .Ready
     }
 }

}

func buttonTapped() {
 if state == .Ready {
    showNext()
 } else {
    state = .ShowNext
 }
}

关于Swift 3 : get information from server in ViewDidLoad and when click a button, 等待服务器的信息响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42546485/

相关文章:

ios - 在 mac os Sierra 上构建tensorflow示例ios代码时出错

java - Python 通过异步递增变量运行 Java 命令行参数。

spring - @BeforeStep 未在 AsyncProcessor 中调用

ios - 如何自动调整单元格大小?

iphone - 为 App Store 和 Enterprise Distribution 构建应用程序

swift - 通过类注入(inject)引用 Observable

c++ - 如何在 Xcode 中使用 OpenCV 3.4.14 识别 .mp4 视频文件的编解码器?

c# - ASP.NET 如何根据另一个页面的单击事件更新页面

ios - 如何更改我在tableView中设置的图像(_ :willDisplayHeaderView:forSection:)?

objective-c - Xcode 6 没有本地化 Interface Builder