1

我有一個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

在'其他'部分,您沒有關閉待處理的控制器。 –

回答

1

在蘋果文檔中,它聲明不應修改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

3

嘗試關閉待處理的警報並顯示下一個沒有動畫的警報。

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

我結束了所有其他條件在完成塊,它的工作,我會嘗試你的解決方案以及 – kareem

+0

非常醜但有效 – Santiago