如何让用户更改密码,邮件,用户名等。任何想法?到目前为止,我有这个代码,你可以在下面看到。我的问题是旧数据不会显示,我无法更新。我会喜欢一些帮助! :)非常感谢
#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
答案 0 :(得分:1)
您的代码看起来很好。只有这应该有效:
[PFUser currentUser].password = someTextField.text;
但有时XCode缓存数据。清理您的项目,然后再次尝试构建。
答案 1 :(得分:0)
不要查询用户对象。您已在[PFUser currentUser];
中拥有该内容。您可以直接从此对象获取字段:
PFUser curUser = [PFUser currentUser];
_usernameField.text = [curUser valueForKey:@"username"];
或只是
_usernameField.text = curUser[@"username"];
你的updateSave
方法是正确的,但我想知道你为什么要使用“newmail”,“newpassword”等。我猜你想在这里使用“密码”,“用户名”等,除非你想在用户个人资料中设置一个名为“newname”的字段......