验证方法不会被调用,并且不会触发segue

时间:2013-09-08 16:01:27

标签: ios objective-c segue

我正在尝试使用解析后端实现注册过程。我有一个名为processFieldEntries的验证方法,一旦启用了完成按钮,我尝试触发我从视图控制器(而不是从完成按钮)以模态方式设置的视图从视图确实出现方法,但验证方法都没有被调用,也没有塞格被触发。我设置了一些调试和记录断点用于调试,但是,我不能再远离它看不到视图加载的事实。我也尝试从完成按钮设置segue。当我这样做时,segue被触发,而不是来自代码,而是来自故事板my storyboard here。如果有人可以帮我弄清楚如何调用processfieldentriees和segue,我真的很感激。谢谢。

NewUserSignUpViewController.h

#import <UIKit/UIKit.h>
#import "ProfileViewController.h"

@interface NewUserSignUpViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UIBarButtonItem *barButtonItem;
@property (strong, nonatomic) IBOutlet UITextField *usernameField;
@property (strong, nonatomic) IBOutlet UITextField *passwordField;
@property (strong, nonatomic) IBOutlet UITextField *repeatPasswordField;
- (IBAction)doneEvent:(id)sender;
- (IBAction)cancelEvent:(id)sender;

@end

NewUserSignUpViewController.m

#import "NewUserSignUpViewController.h"
#import "ProfileViewController.h"
#import <Parse/Parse.h>
#import "ActivityView.h"
@interface NewUserSignUpViewController ()
-(void)processFieldEntries;
- (void)textInputChanged:(NSNotification *)note;
- (BOOL)shouldEnableDoneButton;
@end

@implementation NewUserSignUpViewController
@synthesize barButtonItem = _doneButtonInTheBar;


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

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textInputChanged:) name:UITextFieldTextDidChangeNotification object:_usernameField];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textInputChanged:) name:UITextFieldTextDidChangeNotification object:_passwordField];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textInputChanged:) name:UITextFieldTextDidChangeNotification object:_repeatPasswordField];



}
-(void)viewDidAppear:(BOOL)animated
{
    [_usernameField becomeFirstResponder];
    [super viewDidAppear:animated];
    //perform the segue
    if (_doneButtonInTheBar.enabled == YES) {
        [self performSegueWithIdentifier:@"segueToProfileView" sender:self];
    }



}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {

}

#pragma mark - UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{   textField.delegate = self;
    if (textField == _usernameField) {[_usernameField becomeFirstResponder];}
    if (textField == _passwordField){[_passwordField becomeFirstResponder];}
    if (textField == _repeatPasswordField)
    {

        [_repeatPasswordField becomeFirstResponder];
        [self processFieldEntries];
    }
    return YES;
}
-(BOOL)shouldEnableDoneButton
{
    BOOL enableDoneButton = NO;

    if (_usernameField.text != nil && _usernameField.text.length != 0 &&_passwordField.text != nil &&
        _passwordField.text.length !=0 && _repeatPasswordField.text != nil &&
        _repeatPasswordField.text.length != 0) {
        enableDoneButton = YES;
        [self processFieldEntries];


    }
    return enableDoneButton;
}

-(void)textInputChanged:(NSNotification *)note
{
    _doneButtonInTheBar.enabled = [ self shouldEnableDoneButton];
}
- (IBAction)doneEvent:(id)sender {
    [_usernameField resignFirstResponder];
    [_passwordField resignFirstResponder];
    [_repeatPasswordField resignFirstResponder];
    NSLog(@"processfieldentries");
    [self processFieldEntries];
}

- (IBAction)cancelEvent:(id)sender {
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)processFieldEntries
{
    // Check that we have a non-zero username and passwords.
    // Compare password and passwordAgain for equality
    // Throw up a dialog that tells them what they did wrong if they did it wrong.
    NSString *username = _usernameField.text;
    NSString *password = _passwordField.text;
    NSString *passwordAgain = _repeatPasswordField.text;
    NSString *errorText = @"Please ";
    NSString *usernameBlankText = @"enter a username";
    NSString *passwordBlankText = @"enter a password";
    NSString *joinText = @", and ";
    NSString *passwordMismatchText = @"enter the same password twice";

    BOOL textError = NO;
// Messaging nil will return 0, so these checks implicitly check for nil text.

if (username.length == 0 ||  password.length == 0 || passwordAgain.length == 0) {
    textError = YES;
    //setting the keyboard for th first missing output
    if (passwordAgain.length == 0) {
        [_repeatPasswordField becomeFirstResponder];
    }
    if (password.length == 0) {
        [_passwordField becomeFirstResponder];
    }
    if (username.length == 0) {
        [_usernameField becomeFirstResponder];
    }

    if (username.length == 0) {
        errorText = [errorText stringByAppendingString:usernameBlankText];
    }

    if (password.length == 0 || passwordAgain.length == 0) {
        if (username.length == 0) { // We need some joining text in the error:
            errorText = [errorText stringByAppendingString:joinText];
        }
        errorText = [errorText stringByAppendingString:passwordBlankText];
    }
}else if ([password compare:passwordAgain] != NSOrderedSame)
{errorText = [errorText stringByAppendingString:passwordMismatchText];
    [_passwordField becomeFirstResponder];}
    if (textError) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:errorText message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alertView show];
        return;
        // Everything looks good; try to log in.
        // Disable the done button for now.
        _doneButtonInTheBar.enabled = NO;

        ActivityView *activityView = [[ActivityView alloc]initWithFrame:CGRectMake(0.f, 0.f, self.view.frame.size.width, self.view.frame.size.height)];
        UILabel *label = activityView.label;
        label.text = @"signing up";
        label.font = [UIFont boldSystemFontOfSize:20.0f];
        [activityView.activityIndicator startAnimating];
        [activityView layoutSubviews];
        [self.view addSubview:activityView];

        // Call into an object somewhere that has code for setting up a user.
        // The app delegate cares about this, but so do a lot of other objects.
        // For now, do this inline.
        NSLog(@"does it reach here");
        PFUser *user = [PFUser user];
        user.username = username;
        user.password = password;
        [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[[error userInfo] objectForKey:@"error"] message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
                [alertView show];
                _doneButtonInTheBar.enabled = [self shouldEnableDoneButton];
                [activityView.activityIndicator stopAnimating];
                [activityView removeFromSuperview];
                // Bring the keyboard back up, because they'll probably need to change something.
                [_usernameField becomeFirstResponder];
                return;
            }
            // Success!
            [activityView.activityIndicator stopAnimating];
            [activityView removeFromSuperview];
            }];

    }

}
@end

1 个答案:

答案 0 :(得分:0)

您可以尝试不在performSegueWithIdentifier内使用viewDidAppearperformSegue实际上将您带到另一个ViewController)。相反,您可以使用相同的方法IBAction从连接到完成按钮的processFieldEntries方法调用它。我希望这可以帮助你:))

相关问题