简体   繁体   English

按下UIButton后,Swift UILabel以编程方式更新

[英]Swift UILabel Programmatically Updates after UIButton Pressed

I am looking to program a UILabel to give confirmation if signup was successful or unsuccessful. 我希望对UILabel进行编程,以确认注册成功还是失败。 I am lost on how to go about creating it. 我迷失了如何去创造它。 I have a label placed in the SignUpViewController. 我在SignUpViewController中放置了一个标签。 I am lost on where to go about setting up the code to give the feedback though. 我迷失在哪里可以设置代码以提供反馈。 Please let me know how to go about doing this. 请让我知道如何执行此操作。 Thank You in advanced. 在此先感谢您。

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.
    }
    */

}
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)

        }
    }

Hope this helps.. :) 希望这可以帮助.. :)

pahser.signUpInBackgroundWithBlock{ calls the completion block when the signup api call returns whether its failure or success. 当注册api调用返回失败还是成功时, pahser.signUpInBackgroundWithBlock{调用完成块。 You can set your label value inside that block. 您可以在该块中设置标签值。
Also the UI updates need to be done in the main queue. UI更新也需要在主队列中完成。
You can check out arthankamal's answer if you want to delay the presentViewController function call. 如果要延迟presentViewController函数调用,可以查看arthankamal的答案。
But normally it's better to indicate the user only if there is a failure. 但是通常最好仅在出现故障时指示用户。

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"
        });
     }
}

Solution 1: Delay your presentViewController function within the block, check this link for how to delay, dispatch_after - GCD in swift? 解决方案1:presentViewController块内延迟presentViewController函数,检查此链接以了解如何延迟, dispatch_after-快速运行GCD?

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)
}

Solution 2: Use Toast View, check this github project https://github.com/Rannie/Toast-Swift 解决方案2:使用Toast View,检查此github项目https://github.com/Rannie/Toast-Swift

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM