Swift 按下 UIButton 后 UILabel 以编程方式更新
Swift UILabel Programmatically Updates after UIButton Pressed
我正在寻找一个 UILabel 程序来确认注册是否成功。我迷失了如何去创造它。我在 SignUpViewController 中放置了一个标签。不过,我不知道该去哪里设置代码来提供反馈。请让我知道如何去做。提前谢谢你。
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)
}
}
希望这对您有所帮助..:)
pahser.signUpInBackgroundWithBlock{
在注册 api 调用 returns 时调用完成块,无论是失败还是成功。您可以在该块内设置标签值。
此外 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: 在块内延迟 presentViewController
函数,检查此 link 如何延迟,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 Whosebug 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: 使用 Toast View,检查这个 github 项目 https://github.com/Rannie/Toast-Swift
我正在寻找一个 UILabel 程序来确认注册是否成功。我迷失了如何去创造它。我在 SignUpViewController 中放置了一个标签。不过,我不知道该去哪里设置代码来提供反馈。请让我知道如何去做。提前谢谢你。
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)
}
}
希望这对您有所帮助..:)
pahser.signUpInBackgroundWithBlock{
在注册 api 调用 returns 时调用完成块,无论是失败还是成功。您可以在该块内设置标签值。
此外 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: 在块内延迟 presentViewController
函数,检查此 link 如何延迟,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 Whosebug 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: 使用 Toast View,检查这个 github 项目 https://github.com/Rannie/Toast-Swift