2015-09-05 15 views
0

所以我想創建一個註冊頁面,我遇到了一些麻煩。我希望用戶在註冊前填寫所有信息。他們點擊註冊按鈕後,如果他們沒有填寫所有的字段,他們會得到一個UIAlertView錯誤的許多不同的東西。當我點擊註冊時,它會將用戶帶到下一頁,然後顯示UIAlertView,我不希望發生這種情況。我無法弄清楚發生了什麼事。如果UIAlertView出現,如果他們沒有用戶名或電子郵件填寫我需要UIAlertView顯示後點擊按鈕,而不是轉到下一頁。我的代碼中有一個performSegue方法,但它首先執行,而不是最後一個。有誰能夠幫助我?我還將包括我的故事板的截圖。
The image shows the next view, as well as the segue Identifier解析註冊前往下一個視圖控制器,如果字段丟失

user.signUpInBackgroundWithBlock { 
     (succeeded: Bool, error: NSError?) -> Void in 
     if let error = error { 
      let errorString = error.userInfo?["error"] as? NSString 
      if fullname.isEmpty || email.isEmpty || username.isEmpty || password.isEmpty { 
       var emptyFields:UIAlertView = UIAlertView(title: "Plese try again", message: "It looks like you forgot to fill out all the fields. Please make sure all the fields are filled out so we can create an account for you.", delegate: self, cancelButtonTitle: "Try again") 

       emptyFields.show() 
      } else if error.code == 203 { 
       var takenEmail:UIAlertView = UIAlertView(title: "Please try again", message: "It looks like that email has already been taken, please try again.", delegate: self, cancelButtonTitle: "Try again") 
      } else if error.code == 202 { 
       var usernameTaken:UIAlertView = UIAlertView(title: "Please try again", message: "It looks like that username is already in use, please try again.", delegate: self, cancelButtonTitle: "Try again") 
      } 
     } 
    } 

回答

2

有幾種方法可以做到這一點。我會這樣做的方式如下。

第1步。 好吧,你要選擇註冊視圖控制器。 Step 1 第2步。 您將控制單擊並從黃色視圖控制器圖標拖動到第二個視圖控制器。在出現的菜單中選擇Show Or Present Modally Option。

Step 2 步驟3. 選擇剛纔創建的Segue公司,並改變其標識,在右側面板中「signupSuccessful」。 Step 3

第4步。 您可以像這樣設置代碼。

if usernameTF.text.isEmpty || passwordTF.text.isEmpty { 
      var emptyFields = UIAlertView() 
      emptyFields.title = "Plese try again" 
      emptyFields.message = "It looks like you forgot to fill out all the fields. Please make sure all the fields are filled out so we can create an account for you." 
      emptyFields.addButtonWithTitle("Try Again!") 
      emptyFields.show() 
     }else if usernameTF.text == "ERROR CODE 203" { 
      //do same thing with the alert view! 
     }else if usernameTF.text == "ERROR CODE 202" { 
      //Do another alert view here 
      //You can keep adding the "else ifs" for any other error codes 
     } 
     //Here is where it will send to the second view when everything above is false! 
     else { 
      //Here we present the second view. 
      self.performSegueWithIdentifier("signupSuccessful", sender: self) 
     } 

你走了。 如果您需要再次查看segue或以上任何代碼,請下載該項目。

https://github.com/bobm77/ParseSegueExample

+0

道森我有一個問題,並想知道你是否可以幫助我。我做了你告訴我的一切,並且仍然出現錯誤,如果有辦法我可以聯繫你?電子郵件,Skype等? –

+0

雅檢查你的Twitter我給你發了一條消息。 –

0

在您的視圖控制器實現該方法func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool並返回false如果你不想順着接下去發生。

+0

我把這個ViewController.swift而不是SignupViewController.swift? –

+0

沒有把它放在SignupViewController.swift當你點擊按鈕時,它會在做任何事之前調用這個方法。如果你返回true,它會執行segue並推送ViewController,如果你返回false它會中止segue。 – deadbeef

+0

func signupSuccessful(){ shouldPerformSegueWithIdentifier(「successfulSignup」,sender:self) - > Bool return false } - 爲什麼這不起作用? –

相關問題