2015-01-08 52 views
0

我正在尋找編程UILabel給出確認,如果註冊成功或不成功。我迷失在如何去創造它。我在SignUpViewController中放置了一個標籤。我迷失在設置代碼以提供反饋的地方。請讓我知道如何去做這件事。先進的感謝您。Swift UILabel UIButton Pressed後編程更新

import UIKit 

class signUpViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate { 

    @IBOutlet var usernameTextfield: UITextField! 

    @IBOutlet var emailTextfield: UITextField! 

    @IBOutlet var passwordTextfield: UITextField! 

    @IBOutlet var compasswordTextfield: UITextField! 

    @IBOutlet var birthdateTextfield: UITextField! 

    @IBOutlet var combirthdateTextfield: UITextField! 

    @IBOutlet var confirmationLable: UILabel! 

    @IBAction func signupButton(sender: AnyObject) { 

     var pahser:PFUser = PFUser() 
     pahser.username = usernameTextfield.text 
     pahser.email = emailTextfield.text 
     pahser.password = passwordTextfield.text 

     pahser.signUpInBackgroundWithBlock{ 
      (success:Bool!, error:NSError!)->Void in 
      if error == nil { 
       println("Signup Successfull") 

       var imagePicker:UIImagePickerController = UIImagePickerController() 
       imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 

       imagePicker.delegate = self 

       self.presentViewController(imagePicker, animated: true, completion: nil) 

      }else{ 

       let errorString = error.localizedDescription 
       println(errorString) 

      } 
     } 
    } 


    func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!) { 
     let pickedImage:UIImage = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage 
     let scaledImage = self.scaleImageWith(pickedImage, and: CGSizeMake(100, 100)) 
     let imageData = UIImagePNGRepresentation(scaledImage) 
     let imageFile:PFFile = PFFile(data: imageData) 
     PFUser.currentUser().setObject(imageFile, forKey: "profileImage") 
     PFUser.currentUser().saveInBackground() 

     picker.dismissViewControllerAnimated(true, completion: nil) 

    } 

    func scaleImageWith(newImage:UIImage, and newSize:CGSize)->UIImage{ 
     UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) 
     newImage.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)) 
     UIGraphicsEndImageContext() 

     return newImage 
    } 



    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.passwordTextfield.delegate = self; 
     self.usernameTextfield.delegate = self; 
     self.emailTextfield.delegate = self; 
     self.compasswordTextfield.delegate = self; 
     self.birthdateTextfield.delegate = self; 
     self.combirthdateTextfield.delegate = self 

    } 
     // Do any additional setup after loading the view. 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func textFieldShouldReturn(textField: UITextField!) -> Bool { 
     self.view.endEditing(true); 
     return false; 
    } 

    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

} 

回答

1
pahser.signUpInBackgroundWithBlock{ 
    (success:Bool!, error:NSError!)->Void in 
     if error == nil { 
      println("Signup Successfull") 
      confirmationLable.text = "Signup Successfull" 
      var imagePicker:UIImagePickerController = UIImagePickerController() 
      imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 

      imagePicker.delegate = self 
      dispatch_async(dispatch_get_main_queue()) { 
       self.presentViewController(imagePicker, animated: true, completion: nil) 
      } 


     }else{ 

      let errorString = error.localizedDescription 
      confirmationLable.text = error.localizedDescription 
      println(errorString) 

     } 
    } 

希望這有助於.. :)

+0

您應該在主隊列 – Paulw11

+0

上派發'presentVieController'謝謝。這段代碼工作得很好。 –

0

pahser.signUpInBackgroundWithBlock{調用完成塊時的註冊API調用返回其是否失敗或成功。您可以在該塊內設置標籤值。
另外UI更新需要在主隊列中完成。
如果你想延遲presentViewController函數調用,你可以查看arthankamal的答案。
但通常情況下,最好只在出現故障時指示用戶。

pahser.signUpInBackgroundWithBlock{ 
    (success:Bool!, error:NSError!)->Void in 
    if (error == nil) 
    { 
     dispatch_async(dispatch_get_main_queue(), { 
      confirmationLable.text = "Success" 
     }); 

    } 
    else 
    { 
     dispatch_async(dispatch_get_main_queue(), { 
      confirmationLable.text = "Failure" 
     }); 
    } 
} 
+1

UI任務(如更新標籤文本)必須在主隊列 – Paulw11

+0

是的。謝謝。更新。 :) – rakeshbs

0

解決方案1:延遲塊內的presentViewController功能,檢查此鏈接瞭解如何拖延,dispatch_after - GCD in swift?

pahser.signUpInBackgroundWithBlock{ 
     (success:Bool!, error:NSError!)->Void in 
     if error == nil { 

      confirmationLable.text = "Sign Up Success"; 

      // Delay the Presenting View Controller, 
      // so that you can see that your sign up label success message 
      delay(0.4) { 
       self.presentViewController(imagePicker, animated: true, completion: nil) 
      } 
     }else{ 
      let errorString = error.localizedDescription 
      println(errorString) 
     } 
    } 

// Function To delay your present view controller, from Stackoverflow answer 
func delay(delay:Double, closure:()->()) { 
dispatch_after(
    dispatch_time(
     DISPATCH_TIME_NOW, 
     Int64(delay * Double(NSEC_PER_SEC)) 
    ), 
    dispatch_get_main_queue(), closure) 
} 

解決方案2:使用吐司查看,看看這個GitHub的項目https://github.com/Rannie/Toast-Swift