用另一个UIAlertController替换UIAlertController

问题描述:

我有一个Parse Sign Up,并且我有一个UIAlertController,我希望UIAlertController显示一个指示符,但在注册时出现错误时会被另一个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)

任何想法?谢谢。

+0

在'其他'部分,您没有关闭待处理的控制器。 –

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

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

我认为更好的方法是考虑使用HUD(如https://github.com/jdg/MBProgressHUD)来显示进度或活动,而不是尝试将警报控制器用于非预期目的。

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

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

+0

Rory,我正在调查MBProgress HUD,这正是我想要做的我自己的!我的问题是如何轻松地将它与swift整合? – kareem

+0

我认为您需要做的就是将MBProgressHUD.h和MBProgressHUD.m文件添加到您的项目中。 xcode会问你是否想要创建一个桥接头。然后你可以从swift中使用它。请参阅https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html –

+0

Rory是啊!我使用了cocopods并已经集成在一个测试项目中!它非常整齐,再次感谢你! – kareem

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

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

我结束了所有其他条件在完成块,它的工作,我会尝试你的解决方案以及 – kareem

+0

非常丑但有效 – Santiago