您如何让用户更改密码,邮件,用户名等?有什么想法吗?到目前为止,我有这段代码,您可以在下面看到。我的问题是旧数据没有显示,我无法更新它。我希望获得帮助! :) 非常感谢
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import <MobileCoreServices/UTCoreTypes.h>
#import <QuartzCore/QuartzCore.h>
@interface SettingsUPEViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *usernameField;
@property (strong, nonatomic) IBOutlet UITextField *passwordField;
//@property (strong, nonatomic) IBOutlet UITextField *password2Field;
@property (strong, nonatomic) IBOutlet UITextField *mailField;
- (IBAction)updateSave:(id)sender;
@end
#import "SettingsUPEViewController.h"
@interface SettingsUPEViewController ()
@end
@implementation SettingsUPEViewController
NSArray *retriveFormParse;
NSArray *profileArray;
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
PFQuery *query= [PFUser query];
[query whereKey:@"username" equalTo:[[PFUser currentUser]username]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
profileArray = [[NSArray alloc] initWithArray:objects];
// The find succeeded.
NSLog(@"@%", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(@"%@", objects);
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
PFQuery *query= [PFUser query];
[query whereKey:@"username" equalTo:[[PFUser currentUser]username]];
PFObject *object = [query getFirstObject]; // synchronous request, not ideal, look at getFirstObjectInBackgroundWithBlock
_usernameField.text = [object valueForKey:@"newname"];
_passwordField.text = [object valueForKey:@"newpassword"];
_mailField.text = [object valueForKey:@"newmail"];
_usernameField.delegate = self;
_passwordField.delegate = self;
_mailField.delegate = self;
_usernameField.delegate = self;
_passwordField.delegate = self;
_mailField.delegate = self;
_usernameField.layer.masksToBounds=YES;
_usernameField.layer.borderColor=[[UIColor whiteColor]CGColor];
_usernameField.layer.borderWidth= 1.0f;
_passwordField.layer.masksToBounds=YES;
_passwordField.layer.borderColor=[[UIColor whiteColor]CGColor];
_passwordField.layer.borderWidth= 1.0f;
//_password2Field.layer.masksToBounds=YES;
//_password2Field.layer.borderColor=[[UIColor whiteColor]CGColor];
//_password2Field.layer.borderWidth= 1.0f;
_mailField.layer.masksToBounds=YES;
_mailField.layer.borderColor=[[UIColor whiteColor]CGColor];
_mailField.layer.borderWidth= 1.0f;
_usernameField.delegate = self;
_passwordField.delegate = self;
_mailField.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 18) ? NO : YES;
}
- (void) retriveFromParse {
PFQuery *query= [PFUser query];
[query whereKey:@"username" equalTo:[[PFUser currentUser]username]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(@"@%", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(@"%@", objects);
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
- (IBAction)updateSave:(id)sender {
// Create PFObject with recipe information
PFUser *profile = [PFUser currentUser];
[profile setObject:_usernameField.text forKey:@"newname"];
[profile setObject:_passwordField.text forKey:@"newpassword"];
[profile setObject:_mailField.text forKey:@"newmail"];
// Upload recipe to Parse
[profile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Complete" message:@"Successfully saved profile" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
// Notify table view to reload the profile from Parse cloud
// Dismiss the controller
// [self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:@"nameSet" sender:self];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Failure" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];
}
/// if(![oldPassword.text isEqualToString:[user password]]){
/// [alertMessagePassword setMessage:@"The old password is incorrect!"];
/// [alertMessagePassword show];
#pragma mark - Textfield delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end
最佳答案
不要查询用户对象。您已经在[PFUser currentUser];
中拥有了它,您可以直接从该对象获取字段:
PFUser curUser = [PFUser currentUser];
_usernameField.text = [curUser valueForKey:@"username"];
或简单地
_usernameField.text = curUser[@"username"];
您的
updateSave
方法是正确的,但是我不知道您为什么要使用“ newmail”,“ newpassword”等。我猜想您要在此处使用“ password”,“ username”等,除非您要设置一个字段在用户个人资料上称为“新名称” ...关于ios - 更改密码PFuser,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22781831/