0

我有两个视图控制器用于在我的应用程序中登录和注册。我创建了 UIAlertViews 以在用户未完成该字段或他们的详细信息有问题时提示用户。但是,当在 AlertView 上按确定时,应用程序会将它们带到情节提要中的下一个屏幕,尽管它们的详细信息有任何问题。

用于登录视图的 .m 文件

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

@interface SignInViewController ()

@end

@implementation SignInViewController

- (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.
}

- (IBAction)SignInAction:(id)sender {
    [PFUser logInWithUsernameInBackground:_UsernameField.text  password:_PasswordField.text block:^(PFUser *user, NSError *error) {
    if (!error) {
        NSLog(@"Login User!");
        [self performSegueWithIdentifier:@"signin" sender:self];
        _PasswordField.text = nil;
        _UsernameField.text = nil;
    }
    else {
        NSString *errorString = [[error userInfo] objectForKey:@"error"];
        UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [errorAlertView show];
    }
    }];
 }

@end

这是 LoginView 的 .m 文件

#import "LoginViewController.h"


@interface LoginViewController ()

@end

@implementation LoginViewController
@synthesize scroller;



- (void)viewDidLoad
{

    [super viewDidLoad];
[scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 600)];


}


- (void)viewDidAppear:(BOOL)animated {
PFUser *user = [PFUser currentUser];
if (user.username == nil){
    [self performSegueWithIdentifier:@"login" sender:self];

}
}

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

- (IBAction)RegisterAction:(id)sender {
[_FirstNameField resignFirstResponder];
[_SurnameField resignFirstResponder];
[_EmailField resignFirstResponder];
[_PasswordField resignFirstResponder];
[_ReenterPasswordField resignFirstResponder];
[self checkFieldsComplete];

[self checkFieldsComplete];
}

- (void) checkFieldsComplete {
if ([_FirstNameField.text isEqualToString:@""] || [_SurnameField.text isEqualToString:@""]|| [_EmailField.text isEqualToString:@""] || [_PasswordField.text isEqualToString:@""] || [_ReenterPasswordField.text isEqualToString:@""])  {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops" message: @"Make sure to complete every field" delegate: nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];
    [alert show];
}

else {
    [self checkPasswordsMatch];
}

}

- (void) checkPasswordsMatch {
if (![_PasswordField.text isEqualToString:_ReenterPasswordField.text]) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops" message: @"Passwords don't match" delegate: nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];
    [alert show];
}
else {
    [self registerNewUser];
}
}

- (void) registerNewUser {
NSLog(@"Registering...");
PFUser *newUser = [PFUser user];
newUser.username = _EmailField.text;
newUser.email = _EmailField.text;
newUser.password = _PasswordField.text;

[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (!error) {
        NSLog(@"Welcome to Vici!");
        [self performSegueWithIdentifier:@"login"
                                  sender:self];
    }
    else {
        NSLog(@"There was an error in registration");
    }
}];
}



@end

任何帮助将不胜感激。祝你有美好的一天。

4

1 回答 1

0

如果您在情节提要中将 segue 链接到 UIButton 本身,那么即使使用代码也无法拦截它。

从 UIViewController 本身创建一个 segue,给它一个名字,你需要用代码触发 segue。

于 2013-09-19T07:21:08.610 回答