我有多个视图控制器。
第一个是登录页面,其中包含用户名和密码文本字段。我在这里使用Parse签署用户:
- (IBAction)signup:(id)sender{
PFUser *user = [PFUser user];
user.username = _username.text;
user.password = _password.text;
[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
} else {
[self dismissModalViewControllerAnimated:YES];
NSLog(@"already username");
[self.navigationController popToRootViewControllerAnimated:YES];
_username.textColor = [UIColor redColor];
_inuse.text = @"Username is already taken!";
// Show the errorString somewhere and let the user try again.
}
}];
}
然后在下一个视图控制器上,我想向用户添加信息。
所以我试着在下一个视图控制器上执行此操作。我有两个文本字段firstname
和lastname
:
- (IBAction)signup_name:(id)sender{
PFUser *user = [PFUser user];
user[@"firstname"] = _firstname.text;
user[@"lastname"] = _lastname.text;
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
} else {
[self dismissModalViewControllerAnimated:YES];
NSLog(@"already username");
[self.navigationController popToRootViewControllerAnimated:YES];
_username.textColor = [UIColor redColor];
_inuse.text = @"Username is already taken!";
// Show the errorString somewhere and let the user try again.
}
}];
}
但我收到错误消息:'除非他们已经注册,否则无法保存用户。首先致电signUp。'
我尝试将其添加到- (IBAction)signup:(id)sender
如果没有error
:
[PFUser logInWithUsernameInBackground:_username.text password:_password.text
block:^(PFUser *user, NSError *error) {
if (user) {
// Do stuff after successful login.
} else {
// The login failed. Check error to see why.
}
}];
答案 0 :(得分:1)
注册成功后,[PFUser currentUser]
可以访问当前用户,所以......
- (IBAction)signup_name:(id)sender{
PFUser *user = [PFUser currentUser];
应该工作。