【问题标题】:Replace UIAlertController with another UIAlertController用另一个 UIAlertController 替换 UIAlertController
【发布时间】:2015-04-22 09:18:09
【问题描述】:

我有一个 Parse Sign Up,我有一个 UIAlertController,我希望 UIAlertController 显示一个指示器,但在注册时出现错误时被另一个 UIAlertController 解散。

我有几个注册失败的案例,所有这些都是在添加带有指示器的 UIAlertController 之前完成的。并且当注册成功并且它被正确地解除时,指示器警报控制器工作正常。

//create an alert controller
    let pending = UIAlertController(title: "Creating New User", message: nil, preferredStyle: .Alert)

    //create an activity indicator
    let indicator = UIActivityIndicatorView(frame: pending.view.bounds)
    indicator.autoresizingMask = .FlexibleWidth | .FlexibleHeight
    //add the activity indicator as a subview of the alert controller's view
    pending.view.addSubview(indicator)
    indicator.userInteractionEnabled = false // required otherwise if there buttons in the UIAlertController you will not be able to press them
    indicator.startAnimating()
    self.presentViewController(pending, animated: true, completion: nil)

这是注册的代码,这有效

        if signUpError == nil {
            println("Sign Up Successful")
            // Keep track of the installs of our app
            var installation: PFInstallation =  PFInstallation.currentInstallation()
            installation.addUniqueObject("Reload", forKey: "channels")
            installation["user"] = PFUser.currentUser()
            installation.saveInBackground()


            // to stop the uialertviewcontroller once sign up successful
            pending.dismissViewControllerAnimated(true, completion: {
                self.performSegueWithIdentifier("goToAppFromSignUp", sender: self)

            })

         }

这就是我卡住的地方,这只是其中一种情况,如果我可以让它工作,我可以为其他人做。

        else {
            println("Error Sign Up")


            //If email has been used for another account kPFErrorUserEmailTaken
            if(signUpError!.code == 203) {

                let alertController = UIAlertController(title: "Sign Up Failed", message: "Sorry! Email has been taken! ", preferredStyle: .Alert)


                let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
                    // ...
                }
                alertController.addAction(OKAction)



                    self.presentViewController(alertController, animated: true) {
                        // ...
                    }  
            }

我的想法是关闭 UIAlertController 并在完成块中显示下一个

pending.dismissViewControllerAnimated(true, completion: {
self.presentViewController(alertController, animated: true) {
                // ...
            }

        })

但应用程序冻结在待处理的警报控制器(带有指示器的那个)上。 已经呈现(null)

有什么想法吗?谢谢。

【问题讨论】:

  • 在 'else' 部分,您还没有关闭待处理的控制器。

标签: ios swift parse-platform uialertcontroller presentviewcontroller


【解决方案1】:

尝试关闭待处理的警报并在没有动画的情况下显示下一个警报。

pending.dismissViewControllerAnimated(false, completion: nil)
self.presentViewController(alertController, animated: false, completion: nil)

【讨论】:

  • 我最终只是将所有其他条件放在完成块中并且它有效,我也会尝试您的解决方案
  • 很丑但是很有效
【解决方案2】:

在苹果文档中,它声明您不应修改 UIAlertController 的视图层次结构。这可能会在以后给您带来问题,并且可能是您当前问题的一部分。

在另一个运行时呈现一个也可能会导致您的问题。忽略第一个并在完成第二个演示后理论上应该可以解决您的问题。它挂起的事实表明正在发生其他事情。在关闭它之前,您应该停止并从视图中移除活动指示器。

我认为更好的方法是考虑使用像 https://github.com/jdg/MBProgressHUD 这样的 HUD 来显示进度或活动,而不是尝试将警报控制器用于其不打算用于的目的。

HUD 非常适合在等待事情发生时提示进度或活动。它有许多显示选项,包括带有标题文本的活动。我会在您等待时显示 HUD,然后使用警报控制器显示警报。

我发现使用 HUD 看起来更好,并且更容易控制。

【讨论】:

  • Rory,我正在研究 MBProgress HUD,这正是我自己想要做的!我的问题是如何轻松地将它与 swift 集成?
  • 我认为您需要做的就是将 MBProgressHUD.h 和 MBProgressHUD.m 文件添加到您的项目中。 xcode 会询问您是否要创建桥接头。然后你可以从swift中使用它。见developer.apple.com/library/ios/documentation/Swift/Conceptual/…
  • 罗里耶!我使用 cocopods 并已集成到一个测试项目中!再次感谢您!
猜你喜欢
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
相关资源
最近更新 更多