无法获取方法调用以正确执行

时间:2013-12-30 19:50:44

标签: ios objective-c delegates parse-platform boolean-expression

我正在使用我的应用程序的登录屏幕,无法使其正常运行,并且在过去的2小时内一直在尝试修复此问题。我已将登录屏幕的View Controller设置为两个协议的委托。其中一个协议包含一个可选的BOOL方法,如果用户输入的用户名和密码不符合某些条件,则会停止登录过程。

协议中声明的bool方法如下所示:

- (BOOL)logInViewController:(PFLogInViewController *)logInController     shouldBeginLogInWithUsername:(NSString *)username password:(NSString *)password;

无论如何,当用户输入并提交他们的登录信息时,如果他们输入的用户名和密码的长度不等于0,则登录信息将提交给Web服务器。

但是,如果用户名和密码长度等于0,则BOOL返回NO,并且应该弹出一个标题为“Missing Information”的警告窗口和一条消息“”确保填写所有信息!“

出于某种原因,当我在iPhone上测试并尝试使用空的用户名和密码登录时,它仍然会提交给服务器。它不应该这样做。它应该触发BOOL方法并弹出正确的警报窗口。

我真的很感激帮助。

下面在我的Login View Controller的主文件中,您将看到我在viewDidAppear方法上面实现了BOOL方法,然后实际的方法调用包含在viewDidAppear方法中。

这是我的登录屏幕的主文件:

#import "ParseLoginViewController.h"
#import <Parse/Parse.h>

@interface ParseLoginViewController () <PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate>

@end

@implementation ParseLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.    
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (BOOL)logInViewController:logInController shouldBeginLogInWithUsername:(NSString *)username password:(NSString *)password {
// Check if both fields are completed
if (username && password && username.length != 0 && password.length != 0) {
    return YES; // Begin login process
}

[[[UIAlertView alloc] initWithTitle:@"Missing Information"
                            message:@"Make sure you fill out all of the information!"
                           delegate:nil
                  cancelButtonTitle:@"ok"
                  otherButtonTitles:nil] show];
return NO; // Interrupt login process
}

-(void)viewDidAppear
{
if (![PFUser currentUser]) { // No user logged in
    // Create the log in view controller
    PFLogInViewController *logInViewController = [[PFLogInViewController alloc] init];
    [logInViewController setDelegate:self]; // Set ourselves as the delegate

    // Create the sign up view controller
    PFSignUpViewController *signUpViewController = [[PFSignUpViewController alloc] init];
    [signUpViewController setDelegate:self]; // Set ourselves as the delegate

    // Assign our sign up controller to be displayed from the login controller
    [logInViewController setSignUpController:signUpViewController];

    // Present the log in view controller
    [self presentViewController:logInViewController animated:YES completion:NULL];

    NSString *username = [[NSString alloc]init];
    NSString *password = [[NSString alloc]init];
    PFLogInViewController* logInController = [[PFLogInViewController alloc]init];

    [self logInViewController:logInController shouldBeginLogInWithUsername:username    password:password];

}

}

@end

1 个答案:

答案 0 :(得分:1)

我不确定为什么你有BOOL方法。这样做的简单方法是检查用户名的长度,然后在将POST请求发送到服务器之前检查密码的长度。因此,只要您调用发送帖子请求的方法,只需检查用户名和密码即可。

示例:

-(void)someMethod
{
    //whatever is already here...

    if (username.length && password.length != 0)
        {
            [self callPOSTRequestMethod];
        }
    else
        {
            //create and show your alert...
        }

如果它全部包含在一个VC中,那应该很简单。