【问题标题】:Activity indicator not loading after performSegue to a ViewControllerperformSegue 到 ViewController 后未加载活动指示器
【发布时间】:2018-08-24 07:52:02
【问题描述】:

我是 Swift 的初学者,但我的活动指示器有问题。我一直在使用 NVActivityIndi​​catorView,直到现在我从来没有遇到过任何问题。我有一个 LoginViewController,一旦用户成功登录,它就会指向另一个名为 ContainerViewController 的视图控制器。

登录视图控制器:

    self.networkManager.getConfig(url: url) { (success, message, response) in
    self.stopAnimating()
        if success {
            print(response)

            if (response[APIKey.error] == nil){
                StoreManager.mainview = Definition(definitionDict: response[APIKey.definition] as! NSDictionary)

                let logoutContextMenu = ContextMenu()
                logoutContextMenu.displayName = Constants.logout
                logoutContextMenu.viewName = Constants.logout
                StoreManager.mainview.contextmenu.append(logoutContextMenu)

                Endpoints.org = StoreManager.mainview.org

                self.performSegue(withIdentifier: Constants.containerViewController , sender: self)
            }

            else{
                self.authenticate()
            }
        }else{
            print(message)
            self.showToast(message: Constants.commonErrorMessage)
        }

    }

我的问题是,一旦 segue 完成,我在 ContainerViewController 中的活动指示器在加载 API 时没有显示。这只会在用户登录后发生。

在 ContainerViewController 中,我在 viewWillLoad 中有一个名为 setupUI 的函数,它将调用另一个名为 viewConfig 的函数。 viewConfig 函数内部是活动指示器,每次加载 API 时都会开始动画。

容器视图控制器:

override func viewDidLoad() {
    super.viewDidLoad()

    self.setUpUI()
}

func setUpUI(){
    switch StoreManager.mainview.menuLayout.mobile {
    case Constants.dashboardmenu:
        setUpSideBarMenu()
        if (otherViewNameSelected){
            print(selectedViewName)
            if selectedViewName == ""{
                selectedViewName = StoreManager.mainview.solutions[0].views[0].viewName
            }
            self.getViewConfig(solutionName: StoreManager.mainview.solutions[0].solutionName, viewName: selectedViewName)
        }

        break

    default:
        self.navigationItem.leftBarButtonItem = nil
        break
    }
}

func getViewConfig(){
    let url: String =  Endpoints.view + Endpoints.version + Endpoints.org + "/" + solutionName + "/" + Endpoints.views + viewName + Endpoints.token
    startAnimating()
    networkManager.getConfig(url: url) { (success, message, response) in
        self.stopAnimating()
        if success {
            self.parseViewConfig(response)
        }else{
            print(message)
        }
    }
}

parseViewConfig 是一个带有 tableView 的函数,其中加载数据。

如何在用户登录并显示 ContainerViewController 后显示活动指示器?

【问题讨论】:

  • 你试过用DispatchQueue.main.async { //add or remove active indicator inside dispatch queue }
  • @Shahrukh 我应该把 DispatchQueu.main.async 放在哪里?
  • @FayeRañada 将 startAnimating() 和 stopAnimating() 放入 DispatchQueue.main.async { //在调度队列中添加或删除活动指示器 }
  • @FayeRañada 检查我的答案
  • @Shahrukh 没有任何改变。我仍然有同样的问题。

标签: ios swift segue activity-indicator


【解决方案1】:

试试这个

func getViewConfig(){
    let url: String =  Endpoints.view + Endpoints.version + Endpoints.org + "/" + solutionName + "/" + Endpoints.views + viewName + Endpoints.token
    DispatchQueue.main.async {
        self.startAnimating()
    }
    networkManager.getConfig(url: url) { (success, message, response) in
        DispatchQueue.main.async {
            self.stopAnimating()
        }
        if success {
            self.parseViewConfig(response)
        }else{
            print(message)
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 2013-09-04
    • 1970-01-01
    • 2012-01-25
    相关资源
    最近更新 更多