0

我有两页:

登录页面和注册页面:

对于登录页面,我使用以下内容获取信息并将失败或成功响应传递给用户:

- (void)login:(UIButton *)sender
{
    [self.appDel.api loginWithEmail:self.email.text
                           password:self.password.text
                  completionHandler:^(id response, NSError *error) {
                      if (error) {
                          NSLog(@"ERROR: %@", error);
                      } else {
                          if ([[response objectForKey:Status] isEqualToString:Success]) {
                              self.currentUser = [response objectForKey:kResponseUser];
                              self.currentUserProfile = [response objectForKey:Profile];
                              [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
                          } else {
                              NSLog(@"FAIL: %@", response);
                              [[[UIAlertView alloc] initWithTitle:@"Login Failed"
                                                          message:[response description]
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil] show];
                          }
                      }
                  }];
}

将此用于登录页面,如果用户没有完成两个必需的 texfields 或其中一个填充另一个空并按登录,他将收到失败消息。

但是,在注册的情况下,当文本字段为空并且我们单击注册时程序会崩溃。我应该如何改变这个:

-(void)signUp:(UIButton *)sender
{
    [self.api signup:self.Email.text
                                   password:self.Password.text
                       confirmationPassword:self.ConfirmPassword.text
                                  firstName:self.FirstName.text
                          completionHandler:^(id response, NSError *error) {
    if (error) {
        NSLog(@"ERROR: %@", error);
    } else {
        if ([[response objectForKey:Status] isEqualToString:Success]) {
        self.appDel.theNewUser = [response objectForKey:User];
        self.appDel.theNewUserProfile = [response objectForKey:Profile];
            [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
        } else {
            NSLog(@"FAIL: %@", response);
            [[[UIAlertView alloc] initWithTitle:@"SignUp Failed"
                                        message:[response description]
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
        }
    }
}];

}
4

1 回答 1

0

在您的注册方法中(或者更好的是,在您的 API 类中),检测 nil 和空值。现在的问题是您要在字典中插入一个 nil 值。

这是使用您的代码的示例。我检查是否有任何值长度等于零,如果是,则记录一个小错误。

-(void)signUp:(UIButton *)sender
{

    if ([[[self Email] text] length] == 0 ||
        [[[self Password] text] length] == 0 ||
        [[[self ConfirmPassword] text] length] == 0 ||
        [[[self FirstName] text] length] == 0) {

        // add an error alert here?

        NSLog(@"Hold on there!  You haven't filled everything in.");

        return;

    }

    [self.api signup:self.Email.text
                                   password:self.Password.text
                       confirmationPassword:self.ConfirmPassword.text
                                  firstName:self.FirstName.text
                          completionHandler:^(id response, NSError *error) {
    if (error) {
        NSLog(@"ERROR: %@", error);
    } else {
        if ([[response objectForKey:Status] isEqualToString:Success]) {
        self.appDel.theNewUser = [response objectForKey:User];
        self.appDel.theNewUserProfile = [response objectForKey:Profile];
            [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
        } else {
            NSLog(@"FAIL: %@", response);
            [[[UIAlertView alloc] initWithTitle:@"SignUp Failed"
                                        message:[response description]
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
        }
    }
}];

}
于 2013-02-25T18:37:23.530 回答