【问题标题】:done button begins enabled and disables itself after start typing the username,skips the validation method完成按钮开始启用并在开始输入用户名后自行禁用,跳过验证方法
【发布时间】:2013-09-10 06:59:36
【问题描述】:

我一直在尝试在验证后执行转场,但是一旦我从情节提要设置转场,编译器就会忽略验证和 -(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender 方法并从一开始就启用完成按钮。我试过this,this,this 但我相信我错过了一个关键点。在连接 segue 之前,我能够验证我的注册并且它工作得很好(意味着完成按钮只有在通过验证方法之后才启用,并且我能够注册用户进行解析)。我相信我使用了正确的方法(应该执行segue 方法)但参数错误。因此,如果有人可以帮助我了解我做错了什么,我将不胜感激。谢谢。

SignUpViewController.h

#import <UIKit/UIKit.h>


@interface SignUpViewController : UIViewController<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton;
@property (weak, nonatomic) IBOutlet UITextField *userNameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UITextField *reEnterPasswordField;
- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;

SignUpViewController.m

#import "SignUpViewController.h"
#import <Parse/Parse.h>
#import "ActivityView.h"
#import "ProfileViewController.h"

@interface SignUpViewController ()
- (void)textInputChanged:(NSNotification *)note;
-(void)processFieldEntries;
- (BOOL)shouldEnableDoneButton;
@end

@implementation SignUpViewController

@synthesize doneButton = _doneButton;
@synthesize userNameField = _userNameField;
@synthesize passwordField = _passwordField;
@synthesize reEnterPasswordField = _reEnterPasswordField;


- (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:_reEnterPasswordField];

    _doneButton.enabled = YES;
    NSLog(@"nsnotification is working fine");

}
-(void)viewWillAppear:(BOOL)animated
{
    [_userNameField becomeFirstResponder];
    [super viewWillAppear:animated];
    NSLog(@"indeed usernamefield became a first responder");

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _userNameField ) {
        [_userNameField becomeFirstResponder];
    }
    if (textField == _passwordField) {
        [_passwordField becomeFirstResponder];
    }
    if (textField == _reEnterPasswordField)
    {
        [_reEnterPasswordField becomeFirstResponder];
    }
    NSLog(@"keyboard action works fine ");
    return YES;
}
-(BOOL)shouldEnableDoneButton
{
    BOOL enableDoneButton = NO;

    if (_userNameField.text != nil &&
        _userNameField.text.length > 0 &&
        _passwordField.text != nil &&
        _passwordField.text.length > 0 &&
        _reEnterPasswordField.text != nil &&
        _reEnterPasswordField.text.length > 0)
    {
        enableDoneButton = YES;
        NSLog(@"done button enabled");
    }
    return enableDoneButton;

}
-(void)textInputChanged:(NSNotification *)note
{
    _doneButton.enabled = [self shouldEnableDoneButton];

}
- (IBAction)done:(id)sender {
    [_userNameField resignFirstResponder];
    [_passwordField resignFirstResponder];
    [_passwordField resignFirstResponder];
    [self processFieldEntries];

}

- (IBAction)cancel:(id)sender
{
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

}
-(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 = _reEnterPasswordField.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;
    NSLog(@"validation begins here");

    // 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;
    }
    if (passwordAgain.length == 0) {
        [_reEnterPasswordField becomeFirstResponder];
    }
    if (username.length == 0) {
        [_userNameField becomeFirstResponder];
    }
    if (password.length == 0)
    {
        [_passwordField 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)
    {
        // We have non-zero strings.
        // Check for equal password strings.
        textError = YES;
        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;
    }
    NSLog(@"validation works just fine");

    // Everything looks good; try to log in.
    // Disable the done button for now.
    _doneButton.enabled = NO;
    ActivityView *activityCircle = [[ActivityView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.view.frame.size.width, self.view.frame.size.height)];
    UILabel *label = activityCircle.label;
    label.text = @"Signing You Up";
    label.font = [UIFont boldSystemFontOfSize:20.f];
    [activityCircle.activityIndicator startAnimating];
    [activityCircle layoutSubviews];

    [self.view addSubview:activityCircle];
    NSLog(@"activity view works just fine");

    //parse registeration
    // 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.

    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];
            _doneButton.enabled = [self shouldEnableDoneButton];
            [activityCircle.activityIndicator stopAnimating];
            [activityCircle removeFromSuperview];
            // Bring the keyboard back up, because they'll probably need to change something.
            [_userNameField becomeFirstResponder];
            return;
        }
        // Success!
        [activityCircle.activityIndicator stopAnimating];
        [activityCircle removeFromSuperview];
        //add the next screen here
    }];
    NSLog(@"user signedup just fine");
    //now pass the view from sign up to profile view
}
/*
 //this one didnt work
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    ProfileViewController *myProfileView = [segue destinationViewController];
    if (_doneButton.enabled == YES) {
           [myProfileView performSegueWithIdentifier:@"SignUpSegue" sender:_doneButton];
    }


}
 */
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{   NSLog(@"is this method visible");
    if (_doneButton.enabled == YES) {
        [self performSegueWithIdentifier:@"SignUpSegue" sender:_doneButton];
    }

return NO;

}

 @end

如果我立即点击完成按钮,我看到的日志

2013-09-06 01:20:07.148 nsnotification 工作正常 2013-09-06 01:20:07.155 确实 usernamefield 成为了第一响应者 2013-09-06 01:20:11.290 这种方法是否可见

我在注册过程中看到的日志 2013-09-06 01:22:20.117 nsnotification 工作正常 2013-09-06 01:22:20.120 确实 usernamefield 成为了第一响应者 2013-09-06 01:22:27.701 完成按钮已启用 2013-09-06 01:22:28.094 完成按钮已启用 2013-09-06 01:22:30.196 这种方法是否可见

【问题讨论】:

    标签: ios objective-c segue xcode-storyboard


    【解决方案1】:

    您的完成按钮验证有问题。您没有检查密码重复是否与第一个密码相同。

    另外,请确保在情节提要中默认禁用完成按钮。仅显式启用它。

    【讨论】:

    • 谢谢。我把它放在 processfieldentriees 方法中:else if ([password compare:passwordAgain] != NSOrderedSame)。通过按照您告诉我的操作 + _doneButton.enabled = NO; 我能够解决完成按钮问题在视图中确实加载了。我还修复了 [_passwordField resignFirstResponder];在完成按钮操作中作为 [_reEnterPasswordField resignFirstResponder];但编译器仍然跳过 processfieldentries 方法,所以它不验证,但不知何故,当我开始输入重新输入密码文本字段时,才启用完成按钮。知道为什么吗?
    • 所以我还添加了 [self processFieldEntries];应该启用完成按钮方法,我能够注册用户。知道为什么当我把它放在 done 动作中时它不起作用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多