来自app delegate的stopAnimating活动指示器

时间:2017-09-17 11:37:35

标签: ios swift appdelegate uiactivityindicatorview

我遇到了从我的app委托中的某个函数停止活动指示器的麻烦,我知道该函数已被调用,但我没有收到我日志中的任何错误。

我在signInViewController中创建了activityIndi​​cator,就像这样

@IBAction func googleSignInButton(_ sender: Any) {

    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().signIn()


    activityIndicator.center = self.view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white
    view.addSubview(activityIndicator)

    activityIndicator.startAnimating()
    UIApplication.shared.beginIgnoringInteractionEvents()


}

在我的app委托之后,我有这个功能,

   func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {

            print("this function is running")
            SignInViewController().stopanimating()

            // ...
            if error != nil {
                // ...


                return
            }

我知道这个功能正常工作,因为它在日志中打印文本。并从SignInViewController

调用此函数
func stopanimating() {
        print("stop animating function running")
        DispatchQueue.main.async {
            self.activityIndicator.stopAnimating()
            UIApplication.shared.endIgnoringInteractionEvents()
        }
    }

现在我知道这个函数正在运行,因为它还会在日志中打印预期的文本,而且endIgnoringInteractionEvents也可以工作,但活动指示器仍在运行

我对swift很新,但是之前我从appdelegate操作viewcontrollers中的对象时遇到了问题,这可能吗?

提前致谢

4 个答案:

答案 0 :(得分:2)

这是因为您正在appDelegate中创建SignInViewController的新实例。那是SignInViewController().stopanimating()。您必须调用相同的实例才能停止为活动指示器设置动画。

答案 1 :(得分:1)

这意味着什么 - SignInViewController()。这意味着您正在创建SignInViewController的不同实例,因此在此实例中,您的活动指示器不存在。

解决方案 - 首先解决方案是获取显示活动指标的实例。这意味着你必须得到currentViewController Instance。第二个解决方案是移动你的  func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?)  委托方法到SignInViewController班。

答案 2 :(得分:1)

当你这样做时:

SignInViewController().stopanimating()

您正在做的是创建一个新的SignInViewController并在其上调用该方法。你想要做的是获得现有的停止动画。

一种方法是:

let vc = (GIDSignIn.sharedInstance().uiDelegate as! SignInViewController)
vc.stopanimating()

您可能希望稍后重构,但这应该让您走上正确的道路!

答案 3 :(得分:0)

您需要从子视图中删除活动指示符。 修改您的stopanimating()

func stopanimating() {
        print("stop animating function running")
        DispatchQueue.main.async {
            self.activityIndicator.stopAnimating()
            self.activityIndicator.removeFromSuperview()
            UIApplication.shared.endIgnoringInteractionEvents()
        }
}
相关问题