解析登录无法正常工作

时间:2014-08-04 17:57:32

标签: ios objective-c parse-platform

我正在使用Parse中的代码:

[PFUser logInWithUsernameInBackground:namerL password:passerL
                                block:^(PFUser *user, NSError *error) {
                                    if (user) {
                                        [self performSegueWithIdentifier:@"signinToInbox" sender:self];
                                    } else {
                                        NSLog(@"%@",error);
                                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Failed." message:@"Invalid Username and/or Password." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
                                        [alert show];
                                    }
                                }];

每当我点击登录按钮而不输入任何内容时,它都不会记录任何错误。如果没有这样的用户和/或密码是正确的,我需要将它计为错误。但即使账户细节不正确,它仍然会进入segue。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {我相信它会调用segue,因为它仍然在didSelectRowAtIndexPath中。我只是不知道如何阻止它调用连接它的segue(默认情况下)。

3 个答案:

答案 0 :(得分:0)

这是我使用的代码:

[PFUser logInWithUsernameInBackground:username password:password block:^(PFUser *user, NSError *error) {
    if(!error){
        [self loggedInSuccessfully];
    } else {
        [self loginFailedWithError:error];
    }
}];

在进行任何API调用时,我总是会检查error变量,因为如果它存在则会知道方法失败。

修改

从我们的讨论中看来,当您点击某个单元格时,UITableView执行segue时遇到问题。

可以通过NO方法返回shouldPerformSegueWithIdentifier来禁用segue

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender 
{
    if([identifier isEqualToString:@"signinToInbox"]){
        return YES;
    }
    return NO;
}

segues

之外,这会停用UIViewController上的所有signinToInbox

但是,我相信您的UIStoyboard设置不正确,不应启用默认segue。在调用登录功能后,您只需要在代码中执行segue。因此,在故事板中,您应该使用自定义segue选项。

如果segue已经设置了UIStoryboard,您应该可以点击该Attributes inspector,并在model面板中将下拉列表从custom更改为{{1}}

答案 1 :(得分:0)

也许这个问题根本不是解析登录。从评论中,它似乎是关于附加到表格视图单元格的segues。

如果要有条件地执行segue,则不应将其直接附加到表格视图单元格。而是在IB中的视图控制器之间绘制segue(从视图控制器的状态栏中按住Ctrl键拖动到目标vc)。给那个segue一个名字,比如“goodLogin”,然后在一个条件下手动执行它。

因此,如果您只想在登录成功时发出信号......

// parse login
    if (!error) {
        [self performSegueWithIdentifier:@"goodLogin"];
    } else {
        // don't perform a segue.  Stay here and change ui to indicate an error
    }

答案 2 :(得分:0)

您的segues和表格视图似乎有问题。在Xcode 6中尝试以下操作。 首先删除当前的segue。 按Ctrl +拖动,如图所示: ctrl+drag segue

之后,选择您想要的动作segue(推,模态或自定义)。然后确保标识符与代码中的标识符匹配。

identifier

现在只需在代码中进行以下更改

[PFUser logInWithUsernameInBackground:namerL password:passerL block:^(PFUser *user, NSError *error) {
    if (!error) {
        [self performSegueWithIdentifier:@"signinToInbox" sender:self];
    } else {
        NSLog(@"Error logging in: %@",error);
        [[[UIAlertView alloc] initWithTitle:@"Login Failed" message:@"In valid username and/or password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 
     }
 }];