アプリにログインして登録するための 2 つのビュー コントローラーがあります。ユーザーがフィールドを完了していないか、詳細に問題がある場合にユーザーにプロンプトを表示する UIAlertViews を作成しました。ただし、AlertView で [OK] を押すと、詳細に問題があるにもかかわらず、アプリはストーリーボードの次の画面に移動します。
サインイン ビューの .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
どんな助けでも大歓迎です。良い一日を過ごしてください。