done button begins enabled and disables itself after start typing the username,skips the validation method
我一直想完成后进行跟踪验证,但很快我将遵循从故事情节,只是敷衍的编译器和验证。的方法和
signupviewcontroller.h
1 2 3 4 5 6 7 8 9 10 | #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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | #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 |
我知道如果我点击"日志"按钮,马上做
09年06月01 07.148:20 nsnotification正在精细09年06月01 07.155 20:事实上usernamefield所在的第一反应09年06月01 11.290法:20:这是可见的
如果我去看我的日志通过注册过程09年06月01 20.117 22:nsnotification正在精细09年06月01 20.120 22:事实上usernamefield所在的第一反应09年06月01 27.701 22:完成按钮启用09年06月01 28.094 22:完成按钮启用09年06月01 30.196 22这是可见的方法:
您的"完成"按钮验证错误。您不检查密码重复是否与第一个密码相同。
另外,请确保在情节提要中,默认情况下禁用了"完成"按钮。只显式启用它。